How to manage jobs in Ubuntu: A practical guide

Updated: January 28, 2024 By: Guest Contributor Post a comment

Introduction

Managing jobs in Ubuntu is a foundational skill for system administrators, developers, and power users. This tutorial will guide you through various commands and utilities you can use to effectively manage application processes on your system.

Understanding Jobs and Processes

Before diving into managing jobs, it’s important to differentiate between processes and jobs. A process is an instance of a program running, whereas a job is a process that a shell is aware of and managing. Jobs can be suspended, resumed, and terminated from the shell.

Managing Jobs with Basic Commands

List Active Jobs

jobs

This will list all current jobs with their statuses: running, stopped, or terminated.

`Output:

[1]+  Running                 gedit &
[2]+  Stopped                 firefox

Suspending and Resuming Jobs

To suspend a running job, you can use the Ctrl+Z keystroke:

ping google.com
Ctrl+Z

You’ll see:

[1]+  Stopped                 ping google.com

Resume a job in the foreground with:

fg %1

Or resume a job in the background with:

bg %1

Killing a Job

To terminate a job, use the kill command with the job identifier:

kill %1

The job will terminate, and you’ll receive a confirmation:

[1]+  Terminated              ping google.com

Advanced Job Management with at and cron

The at Command

The at command schedules a job for a one-time execution at a specified time. For example:

echo "uptime" | at now + 5 minutes

The system will execute “uptime” after 5 minutes. The at daemon (atd) must be running for this to work.

The cron Command

The cron daemon is used for scheduling recurring jobs. Modify the crontab file to create a new scheduled job:

crontab -e

You can add this line to create a job that runs every hour:

0 * * * * /path/to/script.sh

Using systemd for Job Scheduling

In Ubuntu, systemd is the init system that manages system processes after booting. It incorporates job scheduling with services and timers.

Creating a Custom Service and Timer

Create a service unit file at /etc/systemd/system/myjob.service:

[Unit]
Description=My custom job

[Service]
Type=oneshot
ExecStart=/path/to/myjob.sh

Create a corresponding timer unit file at /etc/systemd/system/myjob.timer:

[Unit]
Description=Run myjob.service every hour

[Timer]
OnCalendar=*-*-* *:00:00

[Install]
WantedBy=timers.target

Enable and start the timer with:

systemctl enable myjob.timer
systemctl start myjob.timer

Conclusion

This tutorial covered the essentials of job management in Ubuntu, from controlling jobs in the shell to utilizing at, cron, and systemd for scheduling. With practice, these skills will enhance your ability to manage Linux processes efficiently.