How to setup scheduled tasks on web server

Sometimes web developer or web server administrator needs to execute exactly the same server commands regularly. It could be backup procedures, data transfer jobs or any others.

That problem can be resolved using CRON time-based scheduling service. CRON is driven by a configuration file named CRONTAB that specifies shell commands to run periodically on a given schedule. To manage CRON you need have shell access to web server (if you don't have access to shell at web server — you can use Web Console).

Below you can find detailed instructions to setup scheduled tasks on web server:

  • Initially, you must make sure that you have access to CRONTAB. For that just execute following shell command:
    shell> crontab -l
    If CRONTAB is available, you should see output like this:
    No crontab for [your user account name]
    OR (if scheduled task exists):
    30 18 * * * rm -rf /tmp/log/* > /dev/null 2>&1
    30 19 * * * /b.sh > /b.log 2>&1
    If it doesn't work you can try to run CRONTAB using full path:
    shell> /usr/bin/crontab -l
    shell> /usr/sbin/crontab -l
    shell> /usr/local/bin/crontab -l
    shell> /usr/local/sbin/crontab -l
    shell> /bin/crontab -l
    shell> /sbin/crontab -l
    If it still doesn't work, probably server does not have CRON installed on it or you do not have privileges to run it.
    Also you can change user context using commands like su/sudo and check access to CRON under another user account.
  • Next step is set up CRON task to execute some command regularly. For example, to setup scheduled task that will remove all files from directory "/tmp/log/*" daily at "18:30", execute following shell command:
    shell> echo "30 18 * * * rm -rf /tmp/log/* > /dev/null 2>&1" | crontab -
    String "30 18 * * * rm -rf /tmp/log/* > /dev/null 2>&1" is a CRONTAB scheduled task (about CRONTAB syntax you can read here), that string will be redirected into input of command "crontab -" that is waiting a list of CRONTAB scheduled tasks at input.
    Short CRONTAB syntax desciption is:
    # Use the hash sign to prefix a comment
    # +---------------- minute (0 - 59)
    # |  +------------- hour (0 - 23)
    # |  |  +---------- day of month (1 - 31)
    # |  |  |  +------- month (1 - 12)
    # |  |  |  |  +---- day of week (0 - 7) (Sunday = 0 or 7)
    # |  |  |  |  |
    # *  *  *  *  *  [command to be executed]
  • After that, check if that scheduled task is now exists at the list of your CRONTAB schedules, to do that just execute following shell command:
    shell> crontab -l
    If scheduled task exists at the list of your CRONTAB schedules, you should see following output:
    30 18 * * * rm -rf /tmp/log/* > /dev/null 2>&1
    Example you can see at following screenshot:
  • Screenshot: Setting up new scheduled task
  • To set up new CRON task without removing all of your existing scheduled tasks, you can execute following shell command:
    shell> echo -e "`crontab -l`\n30 19 * * * /b.sh > /b.log 2>&1" |
           crontab -
    That command will receive the output from "crontab -l" command, add to it new string "30 19 * * * /b.sh > /b.log 2>&1" (that is new CRONTAB scheduled task), and send all that into input of command "crontab -" that is waiting a list of CRONTAB scheduled tasks at input.
  • Now check if the new scheduled task is added to the existing list of your CRONTAB schedules, to do that just execute following shell command:
    shell> crontab -l
    If new scheduled task is added to the list of your CRONTAB schedules, you should see following output:
    30 18 * * * rm -rf /tmp/log/* > /dev/null 2>&1
    30 19 * * * /b.sh > /b.log 2>&1
    Example you can see at following screenshot:
  • Screenshot: Adding new scheduled task without removing all of existing scheduled tasks

To find more information about crontab command-line parameters just execute following shell command:

shell> crontab --help

Or if you want to read manuals — execute following shell commands:

shell> man crontab
shell> man cron

Using that technology you can execute periodically any shell commands. For example, MySQL database backup or archivate web site content.

DiggDel.icio.usGoogleYahoo MyWebFurlBlinklistRedditMa.gnoliaTechnoratiDiigoStumbleUponSimpy<— Bookmark this page
SPONSORED LINKS
   
Page generation date: 2008-11-19 22:05 GMT
Generation time (sec) / SQL queries: 0.837 / 12
Copyright © 2007-2008 Nick Kovalev, Max Kovalev
Web Console is released under the GNU GPL license