Sling Academy
Home/DevOps/How to manage jobs in Ubuntu: A practical guide

How to manage jobs in Ubuntu: A practical guide

Last updated: January 28, 2024

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.

Next Article: Process priority in Linux: Explained with examples

Previous Article: Ubuntu: How to write an rsync backup script

Series: Linux Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide