Syed Jahanzaib Personal Blog to Share Knowledge !

June 13, 2016

CRON examples ! Focus and save yourself from embarrassment !

Filed under: Linux Related — Tags: , — Syed Jahanzaib / Pinochio~:) @ 3:07 PM

cron examples

 Following commands are made for reference purposes only … zaib


To add scheduled job in linux/ubuntu, use

crontab -e

To view installed cron

crontab -l

Examples:

Run Script after every 5 seconds [Updated 8th Aug, 2016]

I had a UPS monitor script that should run after every 5 seconds to poll the KE input voltage / battery remaining time etc. but as we know that with CRON we can configure interval of minimum 1 minute or above, we cannot set time in seconds. so I make following workaround.

conrtab -e

and added script as follows

# UPMON.SH is my script to monitor UPS
# 10.0.0.1 is my UPS network card
* * * * * /temp/upsmon.sh 10.0.0.1
* * * * * sleep 5; /temp/kemon.sh 10.0.0.1
* * * * * sleep 10; /temp/kemon.sh 10.0.0.1
* * * * * sleep 15; /temp/kemon.sh 10.0.0.1
* * * * * sleep 20; /temp/kemon.sh 10.0.0.1
* * * * * sleep 25; /temp/kemon.sh 10.0.0.1
* * * * * sleep 30; /temp/kemon.sh 10.0.0.1
* * * * * sleep 35; /temp/kemon.sh 10.0.0.1
* * * * * sleep 40; /temp/kemon.sh 10.0.0.1
* * * * * sleep 45; /temp/kemon.sh 10.0.0.1
* * * * * sleep 55; /temp/kemon.sh 10.0.0.1
* * * * * sleep 60; /temp/kemon.sh 10.0.0.1

this way the script will run and wait for five seconds before next execution. 🙂 lalalala / zaib


Run Script at Every 1st day of Month [Every Month]

@monthly /temp/script.sh


Run Script on 1-am on every Monday

0 1 * * MON /temp/xdrive_noupdate.sh


Run Script Daily at 00:00 hours (midnight)

@daily /temp/script.sh


Run Script every hour 

@hourly /temp/script.sh


Run Script every minute

* * * * * /temp/script.sh


Run Script after every 5 minutes

*/5 * * * * /temp/script.sh


Run Script on Specific Timings And Date of Months, Example run script on 10am and 11am  on 12th of every month.

00 10,11 12 * * /temp/script.sh


Run Script on Specific Hours of Every Month, Example run script on 9am of every month.

00 9 10 * * /temp/script.sh


Run Script on Specific Hours RANGES , Example run script on 10am to 8pm , means every hour from 10am-8pm

00 10-20 * * * /temp/script.sh


Shortcuts in CRON

         
@reboot        Run once, at startup.
@yearly        Run once a year, "0 0 1 1 *".
@annually      (same as @yearly)
@monthly       Run once a month, "0 0 1 * *".
@weekly        Run once a week, "0 0 * * 0".
@daily         Run once a day, "0 0 * * *".
@midnight      (same as @daily)
@hourly        Run once an hour, "0 * * * *".


Scheduled CON Job running but not producing results …

[Monday 27th Feb,2017]

I scheduled few scripts on my Ubuntu 14.x box that queries remote servers for storage and send customized reports via email. I can see its execution in /var/log/syslog but the script was not able to query the remote win server.

To solve it I had to add the PATH command in the script , sample as below…

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/temp

this solved the issue 🙂

These are little things that you learn on daily basis ,


Regard’s
Syed Jahanzaib

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: