Linux 在 crontab 中调度 shell 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10757288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:30:34  来源:igfitidea点击:

Scheduling shell script in crontab

linuxshellcrondebiancrontab

提问by DontVoteMeDown

I have a shell script that I can run with rootuser this way:

我有一个 shell 脚本,我可以通过root这种方式与用户一起运行:

root@vivid-15:~# ./backup.sh

root@vivid-15:~# ./backup.sh

It's on /root/backup.sh. Now, how do I schedule it on crontabto execute every day at 01:00 AM? I done this:

它在/root/backup.sh。现在,我如何安排它在crontab每天上午 01:00 执行?我这样做了:

0 1 * * * root

0 1 * * * root

But now I don't know how to proceed the command to do that.

但现在我不知道如何执行命令来做到这一点。

采纳答案by robert

Have you tried this? Also, a "1" in the hour field means 1am, not 1pm.

你试过这个吗?此外,小时字段中的“1”表示凌晨 1 点,而不是下午 1 点。

0 1 * * * root /root/backup.sh

Edit: changed the 13 (1pm) back to 1 (1am).

编辑:将 13 点(下午 1 点)改回 1 点(凌晨 1 点)。

回答by ryanbrownstar

Crontab format:

Crontab 格式:

MIN HOUR DAY MON WEEKDAY CMD

I don't know that you need to define what user you want it to run as when its in crontab -- commands will be run as the user who makes the entries with crontab -e. To create a cron process that runs as root, either login as root or set it up with $ sudo crontab -e

我不知道您需要定义您希望它在 crontab 中运行的用户——命令将作为使用 crontab -e 创建条目的用户运行。要创建以 root 身份运行的 cron 进程,请以 root 身份登录或使用 $ sudo crontab -e 进行设置

I think you're looking for something more like this:

我认为你正在寻找更像这样的东西:

0 1 * * * /root/backup.sh