4

I have a python script and would like to run it every day at 3:00 AM but I can't schedule tasks on linux the same on windows.

I tried to execute the following command in terminal

sudo apt-get install gnome-schedule

he returns me error

E: Unable to find gnome-schedule package

I do not know the reason for this error if there is any linux program to schedule tasks, can anyone recommend me etc ...

my ubuntu is 19.10

:)

1 Answers1

7

The standard Unix/Linux/Ubuntu way of scheduling is using cron. One may also use systemctl, which is way more flexible, but way more complicated.

The cron service is part of the standard install, so you probably already have it. In case you do not, install in the usual way:

sudo apt install cron

To schedule your job, the crontab command will open a schedule in your default editor:

crontab -e

If you have not already chosen an editor, you will be asked to choose one. The easiest for this task will be nano. You will then be editing a file that looks like:

# 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 run
# 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

Note the example in the file above. To run your python script every day at 3am, you would use an entry like:

0 3 * * * python /path/to/my/script

When you exit the editor, the cron job will be set up to run at the next time that fits the criteria. I realize that this solution is very command-line oriented, but that's okay, since you will not be doing this very often. There are many other fancy things you can do with cron, such as running jobs at particular intervals throughout the day. For some helpful examples, have a look at:

man 5 crontab
Fato39
  • 140
Martin W
  • 2,675