How to Set Up Scheduled Tasks on Your Virtual Server


 

Applicable Plans - All Cloud Hosting Plans

How to Set Up Scheduled Tasks on Your Virtual Server

Overview

Scheduled tasks allow you to automate actions on your Virtual Server so that they happen at a specific time or on a specific day or date. Usually these actions relate to programs or applications, such as running a scheduled file copy or restarting a service. Scheduled tasks can also be used to check for application updates or to push updates to users.

Creating scheduled tasks with cron jobs
    Creating cron jobs - basic concepts
    Cron and the user environment
    Cron and file permissions
    Scheduling tasks with cron jobs using a Control Panel
    Scheduling tasks with cron jobs using the command line
        Viewing the existing cron jobs
        Creating or editing cron jobs using the command line
    Cron syntax


Creating scheduled tasks with cron jobs

cron is a time-based job scheduler that allows users to schedule jobs such as shell scripts or commands to run at specific times or dates. With cron, you can schedule a command or script to run at a specific time of day, or on a specific day of the week, or even at a particular time of day on a specific day of the month. The scheduling granularity for cron can be down to the minute, or up to a year.

cron jobs can be scheduled using an available Control Panel, or from the command line of the Virtual Server. cron jobs can be scheduled to run as the root user, or as a regular user. Generally cron jobs for the root user are used for system administration purposes, such as rotating logs or updating software. The cron jobs for a regular user are generally used to run specific commands related to applications or web sites.

This is not a comprehensive guide to using cron. There is an extensive amount of information available online on how to create and manage cron jobs if the information in this user guide does not answer your questions.

Creating cron jobs - basic concepts

Before adding a cron job, you need to understand two basic concepts that affect how cron works. These are the User Environment and Permissions. These concepts apply whether you are adding a cron job as the root user, or as a regular user.

If you find that the cron jobs that you are adding are not working, the problem often comes down to an issue with the user environment or the permissions.

Cron and the user environment

Warning It is important to understand that the cron application DOES NOT inherit the system environment of the user who owns the cron job.

Variables that apply to the user, such as $PATH, do not apply to cron. This means that you will need to use absolute paths for both commands and file locations in your cron jobs. Relative paths will not work.

Here is an example of how the user environment affects cron. As a regular user (in this example, test_user), you have a system path ($PATH) that will search in specific directories for commands. This means that when you want to copy a file with the cp command, the system looks in your path to see if the cp command is there.

-bash-4.1$ whoami
test_user
-bash-4.1$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
-bash-4.1$ which cp
/bin/cp
-bash-4.1$

The example above shows the $PATH for test_user - /usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin. The user can look in any of these directories, which are separated by colons ( : ), for commands.

The cp command is located in the /bin directory, and that directory is in the path for test_user. So when test_user types in cp, the system looks in the path, finds the command, and executes it.


To continue the example, assume that your web site generates a file (file1*) everyday, and you need to copy that file to a directory (directory1*). As a regular user, you could use the command cp file1 directory1, taking advantage of the $PATH to the cp command and also the relative path to the directory location.

-bash-4.1$ cp file1 directory1/
-bash-4.1$ cd directory1/
-bash-4.1$ ll
total 4
-rw-rw-r-- 1 test_user test_user 1891 Aug  9 14:47 file1
-bash-4.1$


However, if you want to call the same cp command to move file1 to directory1 from a cron job, you will need to use the absolute path to the application. This is because cron does not inherit the system path, and does not know where to look for the cp command. You will also need to specify the absolute path to the file being copied, and to the location where the file is being copied to.

This means that the same command, executed by cron, would look like this:

/bin/cp /var/www/test_user/data/file1 /var/www/test_user/data/directory1

You are calling the absolute path to the cp command, the absolute path to the file, and the absolute path to the destination. This is the only way to ensure that cron can find everything it needs.

Cron and file permissions

Warning Unlike the user environment, cron DOES inherit the permissions of the user who created the cron job. This means that the user who created the cron job must have the necessary permissions to run a command or access a file or directory.

For example, a regular user cannot restart a service, only the root user can do that.

-bash-4.1$ whoami
test_user

-bash-4.1$ service crond restart
User has insufficient privilege.

-bash-4.1$

Also, a regular user only has access to files and directories where it has the correct permissions based on its user and group. For example, a regular user cannot copy a file into the root user's directory of /root:

-bash-4.1$ cp file1 /root/
cp: cannot stat /root/file1': Permission denied
-bash-4.1$

When creating your cron jobs, make sure that the user who will own or run the cron job has the correct permissions to access the commands, files, or directories. Some cron jobs cannot be run as a regular user because of this. You will have to create those cron jobs as the root user instead.

Be extremely wary about changing the default system permissions and ownership for files and directories, especially for files and directories outside the user's home directory. The default file and directory permissions are in place for a reason, and changing them can have unintended consequences, such as opening your Virtual Server to malicious hackers, or crashing the Virtual Server.

Scheduling tasks with cron jobs using a Control Panel

You can create and manage cron jobs using your Control Panel.

Creating cron jobs using ISPmanager Control Panel

ISPmanager provides a graphical interface to creating and managing cron jobs, which is available to both the root user and regular users. The location for setting up the cron jobs depends on which version of ISPmanager you have:

Scheduling tasks with cron jobs using the command line

You can create cron jobs from the command line of the Virtual Server, as either the root user or a regular user. To do this, you will need to be able to connect to the VS using SSH, and be able to navigate the Linux file system, use basic commands, and edit files in a text editor (cron uses vim by default).

Viewing the existing cron jobs

To view the existing cron jobs, use the crontab -l command.

[root@example ~]# crontab -l
MAILTO=""
@daily  /bin/cp /root/file1 /root/directory1 >/dev/null 2>&1
10 0 * * *      /usr/local/ispmgr/sbin/cron.sh sbin/mgrctl -m ispmgr task.daily
1       *       *       *       *       /usr/local/ispmgr/sbin/rotate
15      2       *       *       *       /usr/local/ispmgr/sbin/traffic.pl
20 1 * * *      /usr/local/ispmgr/sbin/cron.sh sbin/update.sh ispmgr
*/30    *       *       *       *       /usr/local/ispmgr/sbin/dbcache
[root@example ~]#


As the root user, you can also view the cron jobs for other uses, with the crontab -l -u user_name command.

[root@example ~]# crontab -l -u test_user
MAILTO="test_user@eapps-example.com"
PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
@hourly /bin/cp /var/www/test_user/data/file1 /var/www/test_user/data/directory1
[root@example ~]#

 

Creating or editing cron jobs from the command line

The command to create or edit a cron job is the same: crontab -e. This will open the existing cron file for the user, using the vim editor. As the root user, you can also edit the cron file for another user, with the crontab -e -u user_name command.

MAILTO=""
@daily  /bin/cp /root/file1 /root/directory1 >/dev/null 2>&1
10 0 * * *      /usr/local/ispmgr/sbin/cron.sh sbin/mgrctl -m ispmgr task.daily
1       *       *       *       *       /usr/local/ispmgr/sbin/rotate
15      2       *       *       *       /usr/local/ispmgr/sbin/traffic.pl
20 1 * * *      /usr/local/ispmgr/sbin/cron.sh sbin/update.sh ispmgr
*/30    *       *       *       *       /usr/local/ispmgr/sbin/dbcache


~
"/tmp/crontab.SXgWbz" 7L, 338C

Once you have added your new cron job or made your changes to any existing cron jobs, save and exit the file. This will restart the crond daemon, and your jobs will run when scheduled. Due to the potential complexity of cron, you should not be surprised if you have to adjust your cron job syntax several times before everything works as expected.

Cron syntax

cron has a very unique syntax, which can be somewhat obtuse to parse.

A cron job is read from left to right, starting with minutes, and moving to the actual command. The specific order is:

.--------------- minute (0 - 59)
|  .------------- hour (0 - 23)
|  |  .---------- day of month (1 - 31)
|  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
|  |  |  |  .---- day of week (0 - 6)(Sunday = 0) OR sun,mon,tue,wed,thu,fri,sat
|  |  |  |  |
*  *  *  *  *  command to be executed

This order is absolute - all fields have to be represented, even if you have no value in that field other than an asterisk (*) The asterisk represents all values - for example, if you have an asterisk in the "day of week" field, that means the cron job will run every day of the week.

More information about using cron can be found on the crontab man page, which can be found here - http://www.manpagez.com/man/5/crontab/. You can also view the same man page from the command line of your Virtual Server, with the command man 5 crontab.



Comments

Please login to comment