Scheduled Commands

From Webmin Documentation
(Redirected from ScheduledCommands)
Jump to: navigation, search

In this chapter you can learn about the Unix Cron and At facilities, used for running commands on a regular schedule or once at some future time.

Introduction to Cron jobs[edit]

A Cron job is a Unix term for a command that is run on a regular schedule by the cron daemon. Each job is owned by a Unix user, and runs with the permissions of that user. Each has a set of minutes, hours, days, months and days of weeks on which it runs, allowing considerable flexibility in scheduling. For example, a job may run every 10 minutes, or at 3 am every day, or at 5pm Monday to Friday in January, February and march.

Cron jobs are very useful for performing regular system tasks, such as cleaning up log files, synchronizing the system type, backing up files and so on. Most Linux distributions will have several Cron jobs that were setup by default as part of the operating system install process for doing things like removing unneeded kernel modules, updating the database used by the locate command and rotating log files.

The actual Cron job configuration files are stored in different places, depending on whether they are part of a package included in your Linux distribution or created by a user. The /var/spool/cron directory is for jobs created manually by users, and contains one file per Unix user. The /etc/crontab file and the files under the /etc/cron.d directory contain jobs that are part of packages, such as those that are part of your distribution.

The Scheduled Cron Jobs module[edit]

The Webmin module for editing Cron jobs can be found under the System category. When you enter it, the main page displays a table of all the existing jobs on your system as shown below. For each action, the owner, active status and command are listed. If the command for a job is too long, it will be truncated for display on the page.

The Scheduled Cron Jobs module

Creating a new Cron job[edit]

Using Webmin, you can easily create a new Cron job that will execute as any Unix user on your system. To steps to follow to achieve this are :

  1. On the main page of the module, click on the *Create a new scheduled cron job* link above or below the list of existing jobs. This will take you to the job creation form shown below.
  2. In the Execute cron job as field, enter the name of the Unix user you want the job to execute as. The command executed by the job will run in the user's home directory with his full permissions.
  3. The Active? field can be set to No if you don't want this new job to be actually executed. This is useful for creating jobs to be enabled at a later date.
  4. In the Command field, enter the shell commands that you want the Cron job to run. Just as at the shell prompt, multiple commands can be entered separated by ;, and all the normal shell operators such as &tl, > and && can be used. By default, any output from the command will be emailed to the owner of the Cron job. If you don't want this to happen, make sure that output is redirected to a file or /dev/null.
  5. Anything that you enter into the Input to command field will be fed to the command as input when it is run. If for example your command was mail , anything entered into the field would be sent to that email address.
  6. In the When to execute section, choose the times and dates on which you want the command to execute. For each of the Minutes, Hours, Days, Months and Weekdays options you can choose multiple times or dates, or select the All option. For example, to have a job executed at 3:15am every Monday and Friday, you should change the Minutes option to Selected and select 15, change the Hours option to Selected and select _3_, and the Weekdays option to Selected and select Monday and Friday. The Days and Months options would remain on All.
  7. Click the Create button to add the new Cron job. Assuming there are no errors in your selections, you will be returned to the main page of the module and your new job should appear next to its owner.

The Cron job creation form

Editing a Cron job[edit]

Existing Cron jobs, including those created by users, through Webmin or included with your operating system, can be edited and re-scheduled using this module. Be careful when editing jobs that came as part of your distribution though, as some perform important tasks like truncating webserver, mail and login log files so that they do not use up all of your disk space.

To edit an existing job, the steps to follow are :

  1. On the main page of the module, click on the command for the job that you want to edit. This will take you to the module editing form, which is similar to the image above.
  2. Change any of the details of the job, including the user, command, active status and execution times and dates.
  3. When done, click the Save button, and you will be returned to the main page of the module.

Existing Cron jobs can be deleted by following the steps above, but clicking the Delete button instead of Save. You can also force the immediately execution of a job by clicking the *Run Now* button on the edit page, which will execute the command and display any output that it produces.

Controlling users' access to Cron[edit]

The Scheduled Cron Jobs module can also be used to control access to the crontab command by Unix users at the command line. This can be useful if you all un-trusted users to login to your system, and want to prevent some of them from setting up Cron jobs to run commands and take up CPU time when they are not logged in. Usually by default, all users will have the ability to create Cron jobs, but to change that the steps to follow are :

  1. At the bottom of the module's main page, click on the *Control user access to cron jobs* link. This will take you to a form for entering the usernames of users who can or cannot use Cron.
  2. To grant access to all users, select the Allow all users option. To grant access to only some users, select the *Allow only listed users* option and enter their usernames into the text field. To give access to all except some users, select the Deny only listed users option and enter the usernames of the people that you want to deny access to into the text field.
  3. When done, click the Save button.

If a user has been denied access to Cron, you will no longer be able to use the module to create, edit or delete jobs belonging to him. However, existing jobs may continue to execute!

Module access control options[edit]

As described in Webmin Users, it is possible to use the Webmin Users module to control which Unix users a Webmin user can edit Cron jobs for. To set this up, you must first grant the user access to the module, then follow these steps :

  1. In the Webmin Users module, click on Scheduled Cron Jobs next to the name of the user that you want to restrict.
  2. Change the Can edit module configuration? field to No, so that the user cannot edit the commands that Webmin calls to create and edit jobs.
  3. Switch the Can edit cron jobs for field from All users to one of the other options. The most commonly used is *Users with UID in range*, which allows you to enter a minimum and maximum UID into the fields next to it. Never allow an un-trusted user access to the Cron jobs of system users like root or bin, as this will clearly give him full access to your system and so defeat any other Webmin access control.
  4. Set the Can control user access to cron? *field to *No, so that the Webmin user cannot stop users outside his control using Cron.
  5. Click the Save button at the bottom of the page to make the access control active.

Configuring the Scheduled Cron Jobs module[edit]

Most of the module settings that you can view by clicking on the Module Config link on the main page are set by default to match the installed operating system, and vary rarely need to be changed. However, there is one field that effects the module's user interface, shown in the table below :

Other operating systems[edit]

Cron is available on almost all Unix systems, with very similar capabilities. That means that this module appears almost identical on all operating systems, with only a couple of minor differences. On some, there is no Input to command field available for when creating or editing a job. On others, when controlling which users have access to Cron the default Allow all users option will be replaced with Allow all users except root or *Deny all users*.

Internally, other operating systems use different directories for storing Cron jobs - Solaris for example uses /var/spool/cron/crontabs instead of /var/spool/cron on Linux. Most other systems do not have an /etc/crontab file or /etc/cron.d directory either. However, when using Webmin you do not have to bother about these differences, as it knows about the paths used by other Unix variants and displays all Cron jobs using the same interface, no matter which file they are stored in.

The Scheduled Commands module[edit]

At jobs (called Scheduled Commands by Webmin) are similar to Cron jobs, but instead of executing repeatedly on a schedule run only once at a specified date and time. Unlike Cron jobs, they can be configured to execute in a specific directory instead of the user's home directory. Scheduled commands also keep track of the environment variables that were set when created, and make them available to the command when it runs.

Normally the at command is used to create At jobs, the atq command to list them and the atrm command to remove them. On Linux, the directory /var/spool/at is used to store jobs, one per file. The daemon process atd which runs all the time in background checks these files and runs them at the appropriate times. After a job is run, it is automatically deleted as it is no longer needed.

The Webmin module for creating and deleting At jobs is called Scheduled Commands, and can be found under the System category. When you enter it, the main page will display a list of commands that are waiting to be run (assuming there are any), and a form for adding a new command. The image below shows an example.

The Scheduled Commands module

Any of the commands shown on the main page can be viewed in more detail by clicking on its Job ID. This will take you to a page that shows all the full shell script that will be run when the command executes, including all environment variables. For this page you can cancel the command before it gets a chance to run by clicking the Cancel this command button.

Creating a new scheduled command[edit]

A new command that executes at the time and as the user of your choice can be created by following these steps :

  1. On the main page of the module in the New scheduled command form, enter the name of the user that you want the command to run as into the Run as user field.
  2. Fill in the Run on date and Run at time fields with the date and time that the command is to run at.
  3. Set the Run in directory field to whatever directory you want the command to run in.
  4. In the Commands to execute text box, enter as many shell commands as you want, one per line.
  5. When done, click the Create button. The page will be refreshed and your new command will appear on the list at the top of the page.

Scheduled commands created from within Webmin will use environment variables set by Webmin itself, which are not be the same as the variables that would have been set if the command was created by its owner at the shell prompt.

Cron Jobs Alternatives[edit]

Sometimes external cron job service like built with many enhanced features is handy for use and powerful.