![](/img/trans.png)
[英]Perl code doesn't run in a bash script with scheduling of crontab
[英]crontab doesn't run a bash script
我閱讀了很多其他主題並嘗試了很多東西,但我仍然沒有工作。
我有這個簡單的 run2.sh 腳本:
#!/bin/bash
python3 my_script.py
wait;
sudo mv /home/ubuntu/test_code/csv_created_by_python_script.csv /var/www/html
當我 go 到目錄並寫入時,它可以完美運行
sh run2.sh
但它不會像我想要的那樣運行(每兩個小時)。 我嘗試了某種 crontab,比如
* * * * * /home/ubuntu/test_code/run2.sh
* * * * * PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin /home/ubuntu/test_code/run2.sh
但我想我不明白所有這些路徑的東西......
編輯:cronfile
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# 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
# 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)
#
# m h dom mon dow command
#1 * * * * PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin /home/ubuntu/test_code/run2.sh
* * * * * test_code/run2.sh &>cron.log
* * * * * pwd &>pwd.log
該腳本稱為run.sh
但cron
條目嘗試運行run2.sh
。 cron
條目是否引用了正確的腳本?
您沒有說您正在使用哪個用戶的crontab
。 如果它是針對用戶ubuntu
的,並且這是您用於登錄的用戶 ID,則crontab -e
是編輯 crontab 的命令。 如果您使用sudo
,那么您將影響具有不同權限的root
用戶crontab
。
您不需要兩個條目來實現這一點。 只需刪除最后一行。 我將向您展示如何在下面的crontab
中設置環境變量。
指定* * * * *
表示“每天每分鍾 24/7 運行我的程序”。 要使其每 2 小時運行一次,請使用0 */2 * * *
。 要了解更多信息,請參閱https://crontab.guru/every-2-hours
Cron
從您的主目錄運行命令,因此您可以使用相對路徑在用戶ubuntu
的主目錄下運行程序。
請注意,我設置了所有cron
條目使用的PATH
環境變量,這可能不是必需的,但不應該傷害任何東西:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
0 */2 * * * test_code/run2.sh
要調試,append &>cron.log
像這樣:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
0 */2 * * * test_code/run2.sh &>cron.log
然后從終端類型:
$ tail -f cron.log
讓我知道你是怎么做的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.