Tuesday, January 30, 2018

How To: Schedule Cron Job in Ubuntu

Open a Terminal Window (Command Line) in ubuntu.
Type following command and press Enter.


 crontab -e


This will open editor for you. Write cron command at the end of file.

Use sudo if root privileges required. e.g


 sudo crontab -e


Use following pattern to create new cron job syntax:
  1. The number of minutes after the hour (0 to 59) 
  2. The hour in military time (24 hour) format (0 to 23) 
  3. The day of the month (1 to 31) 
  4. The month (1 to 12) 
  5. The day of the week (0 or 7 is Sun, or use name) 
  6. The command to run 

For example


 0 7 * * * /path/to/your/script.sh


This syntax will run script.sh at 7:00 AM daily.

File must have executable permissions. Run following command to make file executable.


  chmod +x /path/to/your/script.sh


If you want to schedule python file to run via cron, use following command instead.


  0 7 * * * /usr/bin/python3 /path/to/your/pythron-file.py


Just make sure you have already installed python3.

To list existing cron jobs enter following command:


 crontab -l


To remove an existing cron job enter following command:


 crontab -e


Delete the line that contains your cron job and save file.

No comments:

Post a Comment