Blog

03/06/2019

How to run cron job every minute on Linux/Unix



I want to run a cron job that should run a specific shell script /home/jobs/sync.cache.sh every minute. How do I use crontab to execute script every minute on Linux or Unix-like system?

The cron daemon used to execute command or scheduled commands or scripts under Linux or Unix-like system.

Cron is one of the most useful tool in a Linux or UNIX like operating systems. It is usually used for sysadmin jobs such as backups or cleaning /tmp/ directories and more.

Run cron job every minute

The syntax is:
* * * * * /path/to/your/script
To run a script called /home/vivek/bin/foo, type the crontab command:
$ crontab -e
Append the following job:
* * * * * /home/vivek/bin/foo
Save and close the file.

How does it work?

The syntax for crontab is as follows:

* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

The asterisk (*) operator specifies all possible values for a field. For example, an asterisk in the hour time field would be equivalent to every hour or an asterisk in the month field would be equivalent to every month. An asterisk in the every field means run given command/script every minute.

A note about using /etc/cron.d/ directory

If you put cronjob in /etc/cron.d/ directory you must provide the username to run the task as in the task definition:
* * * * * USERNAME /path/to/your/script
For example, run a script that uses rsync to replicate changed files. Create a file named /etc/crond.d/rsync.job
$ sudo vi /etc/crond.d/rsync.job
Append the following:

PATH=/sbin:/usr/sbin:/bin:/usr/bin

# Start job every 1 minute
* * * * * root /root/bin/replication.sh

# Another example
* * * * * root /usr/bin/perl /usr/lib/cgi-bin/check.for.errors.cgi

Save and close the file. Here is a sample /root/bin/replication.sh file:

#!/bin/bash
# Usage: A sample shell script to replicate newly added 
# HTML files/images/js etc on all $servers i.e. poor mans
# file replication service 😉
#
# Author: SXI ADMIN, under GPL v2.0+
#
# Note: Set ssh pub key based auth to work this script
# ------------------------------------------------------------
_rsync="/usr/bin/rsync"
_rsync_opt='-az -H --delete --numeric-ids --exclude=cache/css --exclude=tmp/js'
 
# user name for ssh
u="vivek"
 
# server nodes 
servers="node01 node02"
 
# Source and dest 
S='/home/vivek/wwwfiles/'
D='/home/vivek/wwwfiles'
 
# Let us loop it and do it
for b in ${servers}
do
 ${_rsync} ${_rsync_opt} "$@" ${S} ${u}@${b}:${D}
done

#!/bin/bash # Usage: A sample shell script to replicate newly added # HTML files/images/js etc on all $servers i.e. poor mans # file replication service 😉 # # Author: SXI ADMIN, under GPL v2.0+ # # Note: Set ssh pub key based auth to work this script # ———————————————————— _rsync="/usr/bin/rsync" _rsync_opt=’-az -H –delete –numeric-ids –exclude=cache/css –exclude=tmp/js’ # user name for ssh u="vivek" # server nodes servers="node01 node02" # Source and dest S=’/home/vivek/wwwfiles/’ D=’/home/vivek/wwwfiles’ # Let us loop it and do it for b in ${servers} do ${_rsync} ${_rsync_opt} "$@" ${S} ${u}@${b}:${D} done

A note about dealing with race condition

Modify your script as follows to ensure only one instance of a Bash script is running every minute:

#!/bin/bash
 
## Copyright (C) 2009 Przemyslaw Pawelczyk <przemoc@gmail.com>
##
## This script is licensed under the terms of the MIT license.
## Source https://gist.github.com/przemoc/571091
## https://opensource.org/licenses/MIT
#
# Lockable script boilerplate
 
### HEADER ###
 
LOCKFILE="/var/lock/`basename $0`"
LOCKFD=99
 
# PRIVATE
_lock()             { flock -$1 $LOCKFD; }
_no_more_locking()  { _lock u; _lock xn && rm -f $LOCKFILE; }
_prepare_locking()  { eval "exec $LOCKFD>"$LOCKFILE""; trap _no_more_locking EXIT; }
 
# ON START
_prepare_locking
 
# PUBLIC
exlock_now()        { _lock xn; }  # obtain an exclusive lock immediately or fail
exlock()            { _lock x; }   # obtain an exclusive lock
shlock()            { _lock s; }   # obtain a shared lock
unlock()            { _lock u; }   # drop a lock
 
# Simplest example is avoiding running multiple instances of script.
exlock_now || exit 1
 
### BEGIN OF SCRIPT ###
_rsync="/usr/bin/rsync"
_rsync_opt='-az -H --delete --numeric-ids --exclude=cache/css --exclude=tmp/js'
 
# user name for ssh
u="vivek"
 
# server nodes 
servers="node01 node02"
 
# Source and dest 
S='/home/vivek/wwwfiles/'
D='/home/vivek/wwwfiles'
 
# Let us loop it and do it
for b in ${servers}
do
 ${_rsync} ${_rsync_opt} "$@" ${S} ${u}@${b}:${D}
done
### END OF SCRIPT ###
 
 
 
# Remember! Lock file is removed when one of the scripts exits and it is
#           the only script holding the lock or lock is not acquired at all.

#!/bin/bash ## Copyright (C) 2009 Przemyslaw Pawelczyk <przemoc@gmail.com> ## ## This script is licensed under the terms of the MIT license. ## Source https://gist.github.com/przemoc/571091 ## https://opensource.org/licenses/MIT # # Lockable script boilerplate ### HEADER ### LOCKFILE="/var/lock/`basename $0`" LOCKFD=99 # PRIVATE _lock() { flock -$1 $LOCKFD; } _no_more_locking() { _lock u; _lock xn && rm -f $LOCKFILE; } _prepare_locking() { eval "exec $LOCKFD>"$LOCKFILE""; trap _no_more_locking EXIT; } # ON START _prepare_locking # PUBLIC exlock_now() { _lock xn; } # obtain an exclusive lock immediately or fail exlock() { _lock x; } # obtain an exclusive lock shlock() { _lock s; } # obtain a shared lock unlock() { _lock u; } # drop a lock # Simplest example is avoiding running multiple instances of script. exlock_now || exit 1 ### BEGIN OF SCRIPT ### _rsync="/usr/bin/rsync" _rsync_opt=’-az -H –delete –numeric-ids –exclude=cache/css –exclude=tmp/js’ # user name for ssh u="vivek" # server nodes servers="node01 node02" # Source and dest S=’/home/vivek/wwwfiles/’ D=’/home/vivek/wwwfiles’ # Let us loop it and do it for b in ${servers} do ${_rsync} ${_rsync_opt} "$@" ${S} ${u}@${b}:${D} done ### END OF SCRIPT ### # Remember! Lock file is removed when one of the scripts exits and it is # the only script holding the lock or lock is not acquired at all.

This entry is 15 of 15 in the Linux Cron Jobs Howto & Tutorial series. Keep reading the rest of the series:
  1. What is cron on a Linux or Unix-like systems?
  2. HowTo: Add Jobs To cron Under Linux or UNIX?
  3. Linux Verify crond Daemon And Cronjobs Are Running
  4. Linux Start Restart and Stop The Cron or Crond Service
  5. Linux: List / Display All Cron Jobs
  6. Linux / UNIX Crontab File Location
  7. Linux / UNIX: Change Crontab Email Settings ( MAILTO )
  8. Disable The Mail Alert By Crontab Command On a Linux or Unix-like Systems
  9. Linux: At What Time Cron Entries In cron.daily, cron.weekly, cron.monthly Run?
  10. Linux Execute Cron Job After System Reboot
  11. Linux / UNIX Setup and Run PHP Script As A Cron Job
  12. How to run crontab job every minute on a Linux or Unix-like system
  13. Run crontab (cron jobs) Every 10 Minutes
  14. Cron Job Script Execution on the Last Day of a Month
  15. Run crontab (cron jobs) every 1 minute

(adsbygoogle = window.adsbygoogle || []).push({});

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

20/08/2019

Start your Bitcoin Exchange with our Software

Start your Bitcoin Exchange with our Software Setup your white label bitcoin exchange right away. Or create your own cryptocurrency using...
14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....