linux使用crontab定时任务

本文介绍如何首次配置CronTab定时任务,包括选择编辑器、编辑CronTab文件及常见问题解决方法,如权限不足等。同时提供两种设置每5分钟执行任务的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一次使用crontab 时,会出现

no crontab for root - using an empty one

  

“Select a editor ......”下面有几个选项,就是叫你选择编辑器。(选vim)就可以了,我选的第三个。


但是如果你选错了,就可能会遇到点麻烦,没有办法正常编辑,crontab -e。 怎么办?


执行这个命令:select-editor  (针对crontab的一个命令), 可以让你重新选一次。


分三步走: 

然后执行  1   crontab -e  (linux 命令行输入)

敲击键盘符 a 开始编辑如下



# Edit this file to introduce tasks to be run by cron.

# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task

# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.

# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).

# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/

# For more information see the manual pages of crontab(5) and cron(8)


*/1 * * * * echo “Hello World.” >> /alidata/test

*/1 * * * *  /alidata/tomcatMonitorNew.sh 

~                                                                                                                                                                   
~                                                                                                                                                                       
~                                                                                                                                                                   
~                                                                                                                                                                   
~                                                                                                                                                                   

"/tmp/crontab.D4qssN/crontab" 23L, 961C         


2  ctrl+c   退出编辑 

3  输入:wq 保存退出


在test 文件里面 查看执行结果如下


echo "please enter two number"“Hello World.”
“Hello World.”
“Hello World.”
“Hello World.”
“Hello World.”
“Hello World.”          


继续执行一个sh脚本

echo "please enter two number" >> /alidata/result

发现执行没有效果 单独执行这个文件

提示权限不够

sudo chmod +x xxxx  把xxxx换成那个文件名

再次执行就可以了



ps: 



linux下crontab每隔5分钟执行一次任务的写法

有两种写法


第一种写法是*/5,这种写法有的系统会不支持


*/5 * * * * /xxx/task.sh
第二种写法比较繁琐,但所有系统都支持:


0,5,10,15,20,25,30,35,40,45,50,55 * * * * /xx/task.sh

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值