通过shell脚本运行cron

通过shell脚本运行cron,shell,ubuntu,cron,ubuntu-16.04,Shell,Ubuntu,Cron,Ubuntu 16.04,我有一个shell脚本,名为test.sh 我在这里错过了什么或做错了什么 执行crontab-e后编辑 # 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

我有一个shell脚本,名为test.sh

我在这里错过了什么或做错了什么

执行crontab-e后编辑

# 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)
#
# m h  dom mon dow   command
* * * * * cd /home/seng/Desktop/test.sh

Cron只允许至少1分钟。因此,要安排每10秒一次,您所能做的就是在每10秒运行一次的另一个脚本中调用该脚本。这可能不是正确的解决方案,但符合您的目的

下面是一个简单的脚本,可以完成这项工作:

#!/bin/sh

while true
do

sh test.sh
sleep 10 

done
另外,请记住test.sh执行所需的时间。您可以相应地改变脚本中的睡眠时间

如果您想在一分钟内运行它,那么下面是流程

您需要确保有权运行脚本的用户应该是当前登录的用户

在命令行中输入crontab-e将打开一个编辑器。输入以下内容

****/absolute/path/to/the/script

现在先按ESC键和:wq,然后按ENTER键关闭编辑器

说明:

 .---------------- minute (0 - 59)
 |  .------------- hour (0 - 23)
 |  |  .---------- day of month (1 - 31)
 |  |  |  .------- month (1 - 12)
 |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7)
 |  |  |  |  |
*  *  *  *  * command to be executed
每个astrick值都会被提及,并且可以相应地进行设置

例如:

每分钟运行一次:

* * * * * command to be executed
每天午夜跑步:

0 0 * * * command to be executed
您可以通过执行crontab-l命令列出所有cronjob,并通过crontab-r删除任何cronjob


只需确保还有两件事,要重新启动cron守护程序服务,每次文件更改时都要重新启动cron,并且要使用它执行脚本的权限级别检查/var/mail/日志。

1您不能使用cron根据秒数进行计划;2您没有编辑crontab,只是在命令上随机添加了一个cron表达式line@RobbyCornelissen我应该在哪里添加crontab?在脚本中,在终端中键入crontab-e。将出现一个新窗口。并将以下内容添加到您的crontab文件*****cd/home/seng/Desktop/test.sh以执行minute@JohnJoe:按Esc键并键入:wq您不能在shell脚本中使用不带引号的通配符。可能使用\*或引用此处文档分隔符。另请参见其中关于变量的部分,但通配符也是如此。这充其量只是部分回答。OP还有许多其他问题,其中一些是无关的。是的,正如我已经提到的,这可能不是正确的解决方案,但足以满足要求。但是,您是正确的,因为在shell中,运行脚本的脚本应该一直启动并运行。如果我希望它运行一分钟,应该如何运行?请完成编辑。我已对该过程进行了说明:
* * * * * command to be executed
0 0 * * * command to be executed