Scheduling Tasks

From LQWiki
(Redirected from Schedule TAsks)
Jump to navigation Jump to search

Abstract

You want one (or more) commands be executed at a specific time of the day? For example, a compile job that runs in the night? Or an e-mail that wishes you "good morning"? Or a job that is executed when you login or start your computer? Or a variable to be set during boot time? Then this article is for you.

Scheduling for a specific time

There is a service that is called cron daemon. It can execute any command you want, and is configured with the file crontab. For example, if you want to schedule a task that sends you a mail every day at 3:15 in the morning, add the following line to /etc/crontab:

15 3 * * *   root  ( echo "This is your good morning mail" | sendmail -t yourname@domain.org )

Replace yourname@domain.org with your e-mail-address. Save the file. Then restart the cron daemon with the command

/etc/init.d/cron restart

that's it!

Know

Now, what does this line mean:

15 3 * * *   root  ( echo "This is your good morning mail" | sendmail -t yourname@domain.org )

enclosed in parentheses is the command to be executed. root is the user as of which the command will be executed. 15 3 * * * means: Execute it everytime when the minute is 15, the hour is 3, no matter the day of month, no matter the month, no matter the day of week. The output of this command will be written to /var/spool/mail/root.

Find more help with:

Scheduling at start

There are several possibilities to schedule a task at start:

  • when booting the computer
  • when graphically logging in
  • when logging in to a shell
  • when starting a shell

Scheduling tasks when booting the computer

Every time when you boot your computer, the file /etc/init.d/boot.local should be executed (if existing and executable (see Chmod for setting execution priviledges)). For further information about programming this file, see Bash. When this file is executed, the typical Linux services are still not running. These services are called by the system V init scripts. For example, if you want your computer to send you a mail every time it starts, this init script will depend upon the networking service. You can find your system V init scripts in /etc/init.d. As an example, let's write one sending you a mail. We call it *startmail. It will require the network service to be started - without network, no mailing. So, let's find out how the network service is named on our system:

scorpio:/etc/init.d # ll
total 727
... 
-rwxr-xr-x  1 root root 19922 Sep  9  2005 network
...

So, our network service is called network. We want startmail to start in the runlevels 3 and 5. So, here's the content of /etc/init.d/startmail

### BEGIN INIT INFO
# Provides:       startmail
# Required-Start: network
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Description:    Sends a mail when the computer is started
### END INIT INFO
echo "This is your computer startup mail" | sendmail -t yourname@domain.org

replace yourname@domain.org with your e-mail-address. Permit the execution of this script:

chmod +x /etc/init.d/startmail

Start the program insserv to insert this service into the startup process:

insserv startmail

Now you're done - when you start your computer the next time, it should send you an e-mail.

Scheduling tasks when graphically logging in

When logging in graphically, the scripts of your desktop environment are executed. For example, if your desktop environment is KDE, all scripts in the Autostart path will be executed. You can find and set this path if your point your Konqueror to settings:/System/Paths

Scheduling tasks when logging in

When you log in to a so-called login-shell, scripts are executed automatically. These are not executed if you log in to your desktop environment. Don't mix this up! A login-shell is

  • if you log in using ssh
  • if you log in using su
  • if start your shell as login-shell, e.g. bash --login in case of the Bash

The scripts are typically not executed if you log in to your desktop environment and start a shell by clicking onto its icon. A log in shell inherits all variables from the calling context. The following scripts are executed if they exist and you log in (what logging in is, see above):

  • /etc/profile
  • /etc/profile.d/*.sh (in case of the Sh or the Bash)
  • /etc/profile.d/*.csh (in case of the Csh)
  • All scripts that are executed if start the shell as a non-login-shell.

Scheduling tasks when starting a shell

You can start a shell as a non-login-shell. You do this by calling the shell without any parameter, e.g. bash for executing the Bash. When starting a shell as a non-log-in shell, it inherits the environment variables from the calling context. Besides, some scripts are executed. For example, the Bash executes the file .bashrc in the user's home directory; provided, it exists and is executable.

UseCases

Setting an alias

To set an alias, you do not want a user-specific setting, you want to have your alias globally, for all users and in all shells like ksh and bash, in logIn shells and non-logIn shells. You will need to modify

  • /etc/profile for logIn shells
  • /etc/bash_bashrc for non-logIn bash shells

See also