I read a lot of other topics and try many stuff, but I still doesn't work.
I have this simple run2.sh script:
#!/bin/bash
python3 my_script.py
wait;
sudo mv /home/ubuntu/test_code/csv_created_by_python_script.csv /var/www/html
It works perfectly when I go to the directory and wrote
sh run2.sh
But it won't run like I want (every two hours). I tried some kind of crontab, like
* * * * * /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
But I thinks I don't understand all this path stuff...
EDIT: the 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
The script is called run.sh
but the cron
entry tries to run run2.sh
. Is the cron
entry referring to the proper script?
You did not say which user's crontab
you are working with. If it is for user ubuntu
, and that is the user ID you used to log in, then crontab -e
is the command to edit the crontab. If you use sudo
, then you are affecting the root
user crontab
, which has different permissions.
You do not need two entries to make this happen. Just delete the last line. I will show you how to set an environment variable in a crontab
below.
Specifying * * * * *
means "run my program every minute of every day, 24/7". To make it run every 2 hours instead, use 0 */2 * * *
. To learn more, see https://crontab.guru/every-2-hours
Cron
runs commands from your home directory, so you can use a relative path to run a program under user ubuntu
's home directory.
Notice that I set the PATH
environment variable, used by all cron
entries, which might not be necessary, but should not hurt anything:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
0 */2 * * * test_code/run2.sh
To debug, append &>cron.log
like this:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
0 */2 * * * test_code/run2.sh &>cron.log
Then from a terminal type:
$ tail -f cron.log
Let me know how you do with that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.