Friday, March 14, 2014

Trigger Script Every Minute/Day/Week with Crontab [Linux Ubuntu]

GOAL:  Trigger a script every 15 minutes.

SOLUTION:  
Cron = job scheduler in unix-type operating systems
Crontab = Cron Table file that configures shell commands to run on some schedule

STEPS to add script

1.  Write & Save a series of linux shell/bash commands to script.sh

nano script.sh

*PATHS MUST BE ABSOLUTE, (from root, i.e. /home/user/script.sh)*
Relative Paths Will Not Work (i.e. echo "hi" > test.txt;).  Must be echo "hi" > /home/user/test.txt;

2.  Open Cron

crontab -e

2.  Select 2 for nano (easiest linux file editor)

3.  Add to bottom of file:

#min hour day month weekday command
*/1   *    *    *    *      <command>
example
#Run Script.sh Every 15 minutes
#min hour day month weekday command
*/15   *    *    *    *     /home/user/script.sh

*preceding "sh" is not needed before the script path for cron*

STEPS to check cron status/error/log

1.  Check if cron is running

sudo status cron
2.  Open the system log file (for ubuntu)

nano /etc/rsyslog.d/50-default.conf

3.  Find the line that starts with: #cron.*  Uncomment that line to be:

cron.*

4.  Save the file (Ctrl + Z)

5.  Restart rsyslog

sudo service rsyslog restart

6.  Check cron log file

cat /var/log/cron.log

Also possible to see cron jobs with grep CRON /var/log/syslog
This didn't work for me for Ubuntu 12.04, which is why I wrote this post.

Reference








9 comments:

  1. I have read your blog its very attractive and impressive. I like your blog core Java online training

    ReplyDelete

  2. Nice blog..! I really loved reading through this article. Thanks for sharing such a amazing post with us and keep blogging...


    Best Data Science online training in Hyderabad

    Data Science training in Hyderabad

    Data Science online training in Hyderabad

    ReplyDelete
  3. this is blog is amzing and usfull to every one tqqq u soo much for sharing

    ReplyDelete
  4. Thank u for posting such a valuable information.keep sharing like these.

    http://www.rstrainings.com/core-java-online-training.html

    http://www.rstrainings.com/adv-java-online-training.html

    ReplyDelete
  5. Nice blog..! I really loved reading through this article. Thanks for sharing such a amazing post with us and keep blogging...

    hadoop training in hyderabad
    corejava training in hyderabad

    ReplyDelete