Skip to content

Linux Foundations / Understand the system underneath ยท Step 34 of 38

Lesson: Upstart JobsSwitch
โ† Linux Foundations

System administration

Upstart Jobs

Read and write a simple Upstart job file and control jobs from the command line.

An Upstart job is a text file in /etc/init. The file name, without .conf, is the job name. These examples apply only to systems that run Upstart, such as Ubuntu 14.04 or RHEL 6, and not every feature is the same in all versions.

A small job file

description "Example heartbeat"

start on runlevel [2345]
stop on runlevel [!2345]

respawn

exec /usr/local/bin/heartbeat

Saved as /etc/init/heartbeat.conf, it creates a job called heartbeat.

What the lines mean

  • description is a human readable label.
  • start on lists the events that start the job. Here, entering runlevel 2, 3, 4 or 5.
  • stop on lists the events that stop it. [!2345] means any other runlevel.
  • respawn restarts the process if it dies unexpectedly. Upstart gives up if it respawns too often.
  • exec runs one command as the main process.

Scripts and tasks

When one command is not enough, use a script block. It is run by a shell:

script
    mkdir -p /var/run/heartbeat
    exec /usr/local/bin/heartbeat
end script

A job can have only one main exec or script. Preparation can also go in a pre-start script block.

A task stanza marks a job that is meant to run to completion and exit, such as a cleanup, as opposed to a long running service.

Controlling jobs

$ sudo start heartbeat
heartbeat start/running, process 2201
$ status heartbeat
heartbeat start/running, process 2201
$ sudo stop heartbeat
heartbeat stop/waiting

The same actions work as initctl start heartbeat and so on. There is also restart. After editing a job file, stop the job and start it again so the new settings are used. A plain restart may keep the old definition.

You can check the syntax of a job file with:

$ init-checkconf /etc/init/heartbeat.conf

This tool is available on Ubuntu and is run as a normal user. On Ubuntu, job output is usually logged in /var/log/upstart/<job>.log.

Try it

On a disposable Upstart virtual machine, create a job whose exec line is /bin/sleep 600. Start it, check its status, then stop it. On a modern system, read the file above and explain each stanza aloud instead.

Check your understanding

Upstart Jobs

All 5 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.