crontab 示例

Today we will look into crontab example to execute a script every 5 minutes.

今天,我们将研究crontab示例以每5分钟执行一次脚本。

Crontab示例 (Crontab example)

Recently I installed memcached server on CentOS operating system to use as a caching mechanism for my VBulletin forum. To make sure that it’s running always, I wrote a shell script to check the process status and restart if it’s not running. Now to make sure this script executes at a specific interval, I used crontab.

最近,我在CentOS操作系统上安装了memcached服务器,以用作我的VBulletin论坛的缓存机制。 为了确保它始终在运行,我编写了一个Shell脚本来检查进程状态,如果没有运行则重新启动。 现在,要确保此脚本以特定的间隔执行,我使用了crontab

Crontab示例Shell脚本 (Crontab Example Shell Script)

The shell script is extendable and you can write a similar script to restart any service or process in unix systems.

Shell脚本是可扩展的,您可以编写类似的脚本来重新启动UNIX系统中的任何服务或进程。

Here is the shell script to auto restart service that I am using in my crontab example.

这是我的crontab示例中使用的自动重启服务的Shell脚本。

memcached_restart.sh

memcached_restart.sh

#!/bin/bash
ps -eaf | grep 11211 | grep memcached
# if not found - equals to 1, start it
if [ $? -eq 1 ]
then
/usr/local/bin/memcached -d -u nobody -p 11211 -U 11211 -l 127.0.0.1
else
echo "eq 0 - memcached running - do nothing"
fi

The main command in this script is to check the process, use “ps” command with grep to make sure that it returns only the desired service and returns nothing if the service is not running.

该脚本中的主要命令是检查进程,将“ ps”命令与grep一起使用,以确保它仅返回所需的服务,如果服务未运行则不返回任何内容。

Once you have come up with the command to use, then only thing is to issue a start command to run the service.

一旦提出要使用的命令,那么唯一的事情就是发出启动命令来运行该服务。

Test the script to make sure it’s working as expected before setting it to run at regular intervals using crontab.

在使用crontab将脚本设置为定期运行之前,请对其进行测试以确保其正常运行。

每5分钟Crontab示例 (Crontab every 5 minutes example)

Once you are done with the script, you need to set up a cron job to execute this script at regular interval.
I used below entry to run this script every 5 minutes.

完成脚本后,您需要设置cron作业以定期执行此脚本。
我使用下面的条目每5分钟运行一次此脚本。

*/5 * * * * /Users/pankaj/Downloads/memcached_restart.sh > /Users/pankaj/Downloads/memcache_restart.log 2>&1

Crontab错误–找不到命令 (Crontab Error – command not found)

Note that crontab doesn’t consider environment variables such as PATH, so you have to always provide absolute path for any program or directory location. If you are getting “command not found” error while running a command in a crontab shell script and it runs fine if you execute yourself, then using the relative path will be the problem.

请注意,crontab不考虑诸如PATH之类的环境变量,因此您必须始终为任何程序或目录位置提供绝对路径。 如果在crontab shell脚本中运行命令时遇到“找不到命令”错误,并且如果您自己执行该命令也很好,那么使用相对路径将是问题。

Reference: Wikipedia

参考: 维基百科

翻译自: https://www.journaldev.com/580/crontab-example-every-5-minutes

crontab 示例

crontab 示例_Crontab示例–每5分钟相关推荐

  1. php大于等于符号怎么打出来_PHP常用的特殊运算符号(连续小于符号,三个小于符号,eot,eod,echo示例,print示例)...

    最近在写PHP程序的时候发现了一些特殊的PHP符号,例如连续小于符号,三个小于符号,eot,eod,echo示例,print示例等,突然间发现用这么久的PHP了,竟然连PHP的基本符号都没有认全,看到 ...

  2. java udp 心跳,udp简略通讯示例-心跳示例

    当前位置:我的异常网» 软件架构设计 » udp简略通讯示例-心跳示例 udp简略通讯示例-心跳示例 www.myexceptions.net  网友分享于:2013-09-04  浏览:44次 ud ...

  3. linux cron记录时间,crontab每分钟,5,10分钟,每小时,每天零点,每周日0点,每月1号0点,每年1月1日执行脚本,linux shell定时任务...

    crontab每分钟,5,10分钟,每小时,每天零点,每周日0点,每月1号0点,每年1月1日执行脚本,linux shell定时任务 在linux系统里做维护和开发时, 经常会遇到要用crontabl ...

  4. 示例程序 示例一、由 CLSID 得到 ProgID......

    五.示例程序 示例一.由 CLSID 得到 ProgID.(程序以 word 为例子.如果运行不正确,嘿嘿,你没有安装 word 吧?) ::CoInitialize( NULL );HRESULT ...

  5. python程序示例-python示例程序

    广告关闭 腾讯云双11爆品提前享,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高满返5000元! usrbinpython-- coding: utf-8 --num=[]; i= ...

  6. Linux运维问题解决(1)——Linux 定时任务 crontab 配置及示例

    目录 cron介绍 crontab的使用 实例 linux内置的cron进程能帮我们实现这些需求,cron搭配shell脚本,非常复杂的指令也没有问题. cron介绍 我们经常使用的是crontab命 ...

  7. vlookup示例_VLOOKUP示例–如何在Excel中执行VLOOKUP

    vlookup示例 Microsoft Excel includes a variety of different functions that help users with calculation ...

  8. python软件代码示例-python 示例代码1

    第一章 python基础一 ​在此不再赘述为什么学习python这门编程,网上搜索一箩筐.我在此仅说一句python的好,用了你就会爱上它. 本python示例代码1000+带你由浅入深的了解pyth ...

  9. 【数字信号处理】LTI 系统因果性与稳定性示例 ( 示例一 | 示例二 )

    文章目录 一.系统因果性与稳定性示例一 二.系统因果性与稳定性示例二 一.系统因果性与稳定性示例一 判断系统的 因果性 与 稳定性 : y(n)=1N∑k=0N−1x(n−k)y(n) = \cfra ...

最新文章

  1. Spring Cloud限流详解(附源码)
  2. CG CTF WEB 综合题2
  3. 克鲁斯卡尔(Kruskal)算法求最小生成树
  4. jquery动画 -- 1.加载指示器
  5. Python~win32com~Excel
  6. [MFC]关于Visual studio 2012的AfxGetMainWnd
  7. mysql启动redis_docker一系列mysql,redis,centos啥的启动参数记个笔记
  8. linux设置自启动方式
  9. 【Unity】4.5 树木创建器
  10. 公众号 多服务器配置_多账号公众号分享的素材如何采集以及一键分发?
  11. mac中的echo颜色输出
  12. Redis下载部署并加入idea应用(详细笔记)
  13. 论文word引用参考文献
  14. 个性互踩留言代码: 『寶貝』駕到,{豬頭}.接駕
  15. Blender程序化建模简明教程【PCG】
  16. Unity发布windows程序,Fullscreen Mode设置为Windowed,可运行总是全屏
  17. keil4 #pragma anon_unions
  18. mysql expire_mysql expire_logs_days是怎么生效和计算出来的
  19. 实验:树莓派通过网线连接笔记本实现网络共享
  20. flutter学习笔记--传递信息

热门文章

  1. Ext中的get、getDom、getCmp、getBody、getDoc的区别
  2. [转载] python基于内置的SimpleHTTPServer来搭建简易的FTP服务器实现局域网内文件共享
  3. [转载] python改写二分搜索算法_二分搜索算法模板python实现
  4. CF 398 E(动态规划)
  5. Tomcat 8默认工具manager管理页面访问配置
  6. Springboot 使用wangEditor3.0上传图片
  7. NPM私有服务器搭建方法——sinopia
  8. JavaSE 学习笔记之正则表达式(二十五)
  9. 100. Same Tree (Tree;DFS)
  10. JPA+Hibernate 3.3 ——第一个JPA程序