crontab 示例_Crontab示例–每5分钟

crontab 示例_Crontab示例–每5分钟crontab 示例 Todaywewilll 今天 我们将研究 crontab 示例以每 5 分钟执行一次脚本 Crontab 示例 Crontabexamp RecentlyIins

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)

*/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 示例

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/176438.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月26日 下午9:55
下一篇 2026年3月26日 下午9:56


相关推荐

  • ETH挖矿显卡算力大全[通俗易懂]

    ETH挖矿显卡算力大全[通俗易懂]大家买显卡挖ETH,肯定最关心算力了,这里我整理一版,供大家参考,目前只有主流的整理上了,后期会完善更多的供大家参考!欢迎大家加入大力矿工群:621159725  软件下载:百度云盘链接:https://pan.baidu.com/s/1o9tw41k密码:vkyi…

    2022年6月13日
    65
  • itextpdf生成表格的常见用法

    itextpdf生成表格的常见用法随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)、博主微信(guyun297890152)、QQ技术交流群(183198395)。在前面的文章介绍了itextpdf基本用法和使用itextpdf生成图片,itextpdf还可以实现很多功能,非常强大,今天主要介绍如何使用itextpdf生成表格式的pdf,在实际项目中也非常常用,首先举一个非常…

    2022年6月14日
    39
  • VMware P2V报错

    VMware P2V报错VMwareP2Vwin2008R2操作系统,第一步报错:fault.agentinstallfault.summary,无法在源主机临时安装agent代理,手工将vmware-converter-agent安装程序传到源主机安装,提示报错:error29142couldnotstartservicevstor2mntapi2.0driver(shared),原因是这台源主机以前

    2022年7月16日
    58
  • SuperCLUE多模态视觉评测榜出炉:文心4.5 Turbo并列国内第一!逼近GPT

    SuperCLUE多模态视觉评测榜出炉:文心4.5 Turbo并列国内第一!逼近GPT

    2026年3月12日
    3
  • Cursor整合Qt C++

    Cursor整合Qt C++

    2026年3月16日
    2
  • 卸载MySQL Connector NET无法卸载

    卸载MySQL Connector NET无法卸载卸载MySQLConnectorNET无法卸载最近安装了MYSQL数据库,安装完发现安装在了系统C盘,对于文件整理控加一丢丢强迫症的我来说,太介意了,不能忍。这里是解决办法:安装MYSQL自定义路径方法于是,开始卸载MYSQL,结果,ConnectorNET始终卸载不掉。网上看到了很多方法,删文件删注册表都无济于事,丝毫没有成功。终于,在google里找到可行的解决方法。原文网址:Cannotuninstall/repair/ChangeMySQL-Connector-net?Iss

    2022年7月15日
    33

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号