7

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

Megha Sharma
  • 2,235
  • 8
  • 27
  • 31

2 Answers2

13

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
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
2
crontab -e

add an entry

0 10 * * * sh your_script.sh > your_log_file
Anoop
  • 5,540
  • 7
  • 35
  • 52
  • can you please tell me what is these * mean ? – Megha Sharma Apr 18 '14 at 04:35
  • when I am writing this : `crontab -e no crontab for root - using an empty one` !!! Is this an error – Megha Sharma Apr 18 '14 at 04:36
  • first * means all days of the week, second * means all months in an year, third * means all days of the week . That means we care only about time i.e at 0 mins, 10 hrs irrespective of other constraints. Also I think no crontab for root means there are no cron jobs scheduled. Initial part of the newly created file will be commented, you need to add your job after the comments. – Anoop Apr 18 '14 at 04:39