我正在编写一个脚本来备份一些目录,例如/etc
夜间备份。
如果我手动运行该脚本并输入密码,该脚本可以正常工作,但在 crontab 中启动失败
为了测试目的,我在 ~/ 中创建了一个名为 script.sh 的小脚本,但它也无法启动。
脚本
#!/bin/zsh echo "Salut tout le monde" >> echo.txt 2&> err.txt
sudo crontab -l 看起来像这样
# m h dom mon dow command * 1 1 * * /home/jmaurice/backups/backup_monthly.sh * * * * * ./script.sh
ls -l 脚本.sh
-rwxr-xr-x 1 jmaurice jmaurice 63 Aug 5 09:35 script.sh
journalctl --grep='cron' --no-pager --since='-1hour'
Aug 06 04:15:01 kali CRON[5481]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 04:15:01 kali CRON[5481]: pam_unix(cron:session): session closed for user root Aug 06 04:17:01 kali CRON[6456]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 04:17:01 kali CRON[6457]: (root) CMD (cd / && run-parts --report /etc/cron.hourly) Aug 06 04:17:01 kali CRON[6456]: pam_unix(cron:session): session closed for user root Aug 06 04:25:01 kali CRON[10350]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 04:25:01 kali CRON[10350]: pam_unix(cron:session): session closed for user root Aug 06 04:35:01 kali CRON[15257]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 04:35:01 kali CRON[15257]: pam_unix(cron:session): session closed for user root Aug 06 04:39:01 kali CRON[17217]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 04:39:01 kali CRON[17217]: pam_unix(cron:session): session closed for user root Aug 06 04:40:44 kali sudo[18107]: jmaurice : TTY=pts/0 ; PWD=/home/jmaurice ; USER=root ; COMMAND=/usr/bin/crontab -l Aug 06 04:41:10 kali sudo[18360]: jmaurice : TTY=pts/0 ; PWD=/home/jmaurice ; USER=root ; COMMAND=/usr/bin/crontab -e Aug 06 04:43:01 kali cron[971]: (root) RELOAD (crontabs/root) Aug 06 04:45:01 kali CRON[20342]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 04:45:01 kali CRON[20343]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 04:45:01 kali CRON[20342]: pam_unix(cron:session): session closed for user root Aug 06 04:45:01 kali CRON[20343]: (CRON) info (No MTA installed, discarding output) Aug 06 04:45:01 kali CRON[20343]: pam_unix(cron:session): session closed for user root Aug 06 04:46:09 kali sudo[20942]: jmaurice : TTY=pts/0 ; PWD=/home/jmaurice ; USER=root ; COMMAND=/usr/bin/crontab -e Aug 06 04:47:17 kali sudo[21509]: jmaurice : TTY=pts/0 ; PWD=/home/jmaurice ; USER=root ; COMMAND=/usr/bin/crontab -e Aug 06 04:48:01 kali cron[971]: (root) RELOAD (crontabs/root) Aug 06 04:48:01 kali CRON[21880]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 04:48:01 kali CRON[21880]: (CRON) info (No MTA installed, discarding output) Aug 06 04:48:01 kali CRON[21880]: pam_unix(cron:session): session closed for user root Aug 06 04:49:19 kali sudo[22512]: jmaurice : TTY=pts/0 ; PWD=/home/jmaurice ; USER=root ; COMMAND=/usr/bin/crontab -e Aug 06 04:51:01 kali cron[971]: (root) RELOAD (crontabs/root) Aug 06 04:51:01 kali CRON[23342]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 04:51:01 kali CRON[23342]: (CRON) info (No MTA installed, discarding output) Aug 06 04:51:01 kali CRON[23342]: pam_unix(cron:session): session closed for user root Aug 06 04:52:01 kali CRON[23829]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 04:52:01 kali CRON[23829]: (CRON) info (No MTA installed, discarding output) Aug 06 04:52:01 kali CRON[23829]: pam_unix(cron:session): session closed for user root Aug 06 04:53:01 kali CRON[24329]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 04:53:01 kali CRON[24329]: (CRON) info (No MTA installed, discarding output) Aug 06 04:53:01 kali CRON[24329]: pam_unix(cron:session): session closed for user root Aug 06 04:54:01 kali CRON[24839]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 04:54:01 kali CRON[24839]: (CRON) info (No MTA installed, discarding output) Aug 06 04:54:01 kali CRON[24839]: pam_unix(cron:session): session closed for user root Aug 06 04:55:01 kali CRON[25328]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 04:55:01 kali CRON[25329]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 04:55:01 kali CRON[25329]: (CRON) info (No MTA installed, discarding output) Aug 06 04:55:01 kali CRON[25329]: pam_unix(cron:session): session closed for user root Aug 06 04:55:01 kali CRON[25328]: pam_unix(cron:session): session closed for user root Aug 06 04:56:01 kali CRON[25815]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 04:56:01 kali CRON[25815]: (CRON) info (No MTA installed, discarding output) Aug 06 04:56:01 kali CRON[25815]: pam_unix(cron:session): session closed for user root Aug 06 04:57:01 kali CRON[26362]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 04:57:01 kali CRON[26362]: (CRON) info (No MTA installed, discarding output) Aug 06 04:57:01 kali CRON[26362]: pam_unix(cron:session): session closed for user root Aug 06 04:58:01 kali CRON[26847]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 04:58:01 kali CRON[26847]: (CRON) info (No MTA installed, discarding output) Aug 06 04:58:01 kali CRON[26847]: pam_unix(cron:session): session closed for user root Aug 06 04:59:01 kali CRON[27331]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 04:59:01 kali CRON[27331]: (CRON) info (No MTA installed, discarding output) Aug 06 04:59:01 kali CRON[27331]: pam_unix(cron:session): session closed for user root Aug 06 05:00:01 kali CRON[27846]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 05:00:01 kali CRON[27846]: (CRON) info (No MTA installed, discarding output) Aug 06 05:00:01 kali CRON[27846]: pam_unix(cron:session): session closed for user root Aug 06 05:01:01 kali CRON[28342]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 05:01:01 kali CRON[28342]: (CRON) info (No MTA installed, discarding output) Aug 06 05:01:01 kali CRON[28342]: pam_unix(cron:session): session closed for user root Aug 06 05:02:01 kali CRON[28827]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 05:02:01 kali CRON[28827]: (CRON) info (No MTA installed, discarding output) Aug 06 05:02:01 kali CRON[28827]: pam_unix(cron:session): session closed for user root Aug 06 05:03:01 kali CRON[29313]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 05:03:01 kali CRON[29313]: (CRON) info (No MTA installed, discarding output) Aug 06 05:03:01 kali CRON[29313]: pam_unix(cron:session): session closed for user root Aug 06 05:04:01 kali CRON[29796]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 05:04:01 kali CRON[29796]: (CRON) info (No MTA installed, discarding output) Aug 06 05:04:01 kali CRON[29796]: pam_unix(cron:session): session closed for user root Aug 06 05:05:01 kali CRON[30285]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 05:05:01 kali CRON[30284]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 05:05:01 kali CRON[30285]: (CRON) info (No MTA installed, discarding output) Aug 06 05:05:01 kali CRON[30285]: pam_unix(cron:session): session closed for user root Aug 06 05:05:01 kali CRON[30284]: pam_unix(cron:session): session closed for user root Aug 06 05:05:16 kali sudo[30396]: jmaurice : TTY=pts/0 ; PWD=/home/jmaurice ; USER=root ; COMMAND=/usr/bin/crontab -e Aug 06 05:06:01 kali CRON[30782]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 05:06:01 kali CRON[30782]: (CRON) info (No MTA installed, discarding output) Aug 06 05:06:01 kali CRON[30782]: pam_unix(cron:session): session closed for user root Aug 06 05:07:01 kali CRON[31267]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 05:07:01 kali CRON[31267]: (CRON) info (No MTA installed, discarding output) Aug 06 05:07:01 kali CRON[31267]: pam_unix(cron:session): session closed for user root Aug 06 05:08:01 kali CRON[31754]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 05:08:01 kali CRON[31754]: (CRON) info (No MTA installed, discarding output) Aug 06 05:08:01 kali CRON[31754]: pam_unix(cron:session): session closed for user root Aug 06 05:09:01 kali CRON[32242]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 05:09:01 kali CRON[32243]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0) Aug 06 05:09:01 kali CRON[32242]: pam_unix(cron:session): session closed for user root Aug 06 05:09:01 kali CRON[32243]: (CRON) info (No MTA installed, discarding output) Aug 06 05:09:01 kali CRON[32243]: pam_unix(cron:session): session closed for user root
script.sh 应该每分钟运行一次,但我却没有发现它的踪迹。
有人能指出我这里做错了什么吗?
PS:这个问题已经被错误地发布在 askubuntu 上https://askubuntu.com/q/1480341/1717746
@Rohit Gupta 我做了如下两次尝试,当然是分开的,但都没有成功
> * * * * * /home/jmaurice/script.sh
> * * * * * ./home/jmaurice/script.sh
但无论如何,还是要感谢你的意见,我会尝试在脚本本身中放入绝对路径,但我认为这不会改变什么
答案1
cron/crontab 条目中的所有路径以及从那里运行的脚本都需要具有绝对路径。
这是因为它运行时没有将其主目录设置为您的主目录。
假设这是它所在的路径,将您的条目更改为
* * * * * /home/jmaurice/backups/script.sh