Taming Background Tasks with nohup, disown, and systemd's linger Option

Introduction to Background Tasks

I’ve been in situations where running a command or process in the background is a must - think long-running backups, downloads, or compilations. In those cases, being able to continue working on other tasks is a huge plus. Here, we’ll explore three ways to manage background tasks: nohup, disown, and systemd’s linger option. Each has its own use cases, and I’ll share some practical tips on when to use each.

Using nohup

nohup is a simple command that lets you run a process in the background, ignoring hangup signals. This means you can log out of your terminal, and the process will keep running. To use nohup, just prefix your command with nohup and append an ampersand (&):

nohup command &

For example, running a backup script in the background would look like this:

nohup /path/to/backup/script.sh &

By default, nohup will redirect the output to a file named nohup.out in the current directory. You can change this by redirecting the output to a different file:

nohup command > output.log 2>&1 &

This redirects both standard output and standard error to output.log.

Using disown

disown is a bash built-in command that removes a job from the job table, effectively “disowning” it. The process keeps running, but it’s no longer tied to the current shell session. To use disown, first run your command in the background:

command &

Then, use disown to remove the job from the job table:

disown %1

Replace %1 with the job number, which you can find using the jobs command. I usually start with jobs to get the job number, then use disown to remove it.

Using systemd’s linger Option

Systemd provides a linger option that enables or disables lingering for a user. When lingering is enabled, systemd keeps the user’s session active even after logout. This means background processes started by the user will continue running. To enable lingering for a user, use:

loginctl enable-linger username

Replace username with the actual username. Then, start a background process using systemd-run:

systemd-run --user command

This runs the command in the background, and it keeps running even after logout.

Trade-offs and Considerations

Each method has its own trade-offs. nohup is simple, but less flexible. disown provides more control, but requires more manual work. Systemd’s linger option is convenient, but requires systemd to be installed and configured. Don’t bother with linger if you’re not using systemd.

In terms of security, running background processes can pose risks if not managed properly. For example, a background process running with elevated privileges could be exploited. To mitigate this, use tools like sudo to manage privileges, and regularly monitor your system for suspicious activity.

Troubleshooting and Debugging

If you encounter issues with background processes, tools like ps and top can help. The ps command lists running processes, while top monitors system activity. The systemd documentation has more information on using these tools and configuring background processes. For more information, check out the systemd documentation or the bash manual.


See also