Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to schedule a shell script in Linux centos?

I have a script that I need to schedule on daily basis. How can I schedule a script in centos. Like I want to run that script everyday at 10 AM and can I store the output of the script in a log file.

How to achieve this?

Thanks

like image 440
Megha Sharma Avatar asked Apr 18 '14 04:04

Megha Sharma


2 Answers

To schedule a script, you'll need to use crontab. You can add a schedule entry by doing:

crontab -e

Once you are inside, the cron accepts pattern in the form:

<Minute> <Hour> <Day> <Month> <Day of Week> <Command>

So a correct crontab entry for 10am would be

0 10 * * * /path/to/script

If you want to capture the output you can specify the logfile inside cron like

0 10 * * * /path/to/script > /path/to/log/output.log

Just ensure you put a she-bang header in your script. Alternatively, you can specify the shell interpreter inside cron by saying:

0 10 * * * bash /path/to/script > /path/to/log/output.log
like image 166
jaypal singh Avatar answered Sep 20 '22 19:09

jaypal singh


crontab -e

add an entry

0 10 * * * sh your_script.sh > your_log_file
like image 24
Anoop Avatar answered Sep 21 '22 19:09

Anoop