Schedule tasks

From Linuxintro
(Redirected from Scheduling tasks)


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 ? 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 )

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

/etc/init.d/cron restart

that's it!


Now, what does this line mean:

15 3 * * *   root  ( echo "This is your good morning mail" | sendmail -t )

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:

man cron
man crontab
man 5 crontab

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

There are two ways to execute a command when booting the computer:

  • an entry in /etc/crontab
  • a script under /etc/init.d and are grouped by runlevels. Your distribution will either use the upstart framework or the SysV framework for these and it is hard to tell which one it uses.

The following line in /etc/crontab will execute shellinabox at computer start:

@reboot root /usr/local/bin/shellinaboxd &
SysV init scripts

Every time when you boot your computer, the system V init scripts from /etc/init.d are executed. They are called services and depend on each other. 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

# Provides:       startmail
# Required-Start: network
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Description:    Sends a mail when the computer is started
echo "This is your computer startup mail" | sendmail -t

replace 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 startup procedure of your desktop environment will be executed.

  • the .desktop files from /etc/xdg/autostart
  • for 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.
  • for KDE, the KDE session manager server (ksmserver) may start programs to restore your previous session.
You can configure ksmserver using the command systemsettings -> "Startup and Shutdown" or edit ~/.kde4/share/config/ksmserverrc. Your actual file may be different, to find out your config directory run the command
kde4-config --path config

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.


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 log-in-shells and non-log-in-shells. You will need to modify

  • /etc/profile for log-in-shells
  • /etc/bash.bashrc.local for non-log-in bash shells

See also