BOBOBK

Managing Long-Running Tasks on Unix-like Systems

TECHNOLOGY

Due to daily work all using Linux systems, generally for commands known to run for a long time, nohup, screen, or tmux are used to run the command. However, sometimes after starting a command, you suddenly realize it runs very long. Is there a way to continue working in the current shell and keep the command running after logout until the task finishes? After some searching, a solution was found and is recorded here.

Managing background jobs on Unix-like systems is an important skill to improve work efficiency. This article will guide you on how to use Ctrl+Z, bg, and disown commands to move running jobs to the background and ensure they continue running after logout.

Main steps:

  • Use Ctrl+Z to pause the job:
  • Use bg to move the job to the background:
  • Use disown to keep the job running after logout

Use Ctrl+Z to pause the job:

When a job is running in the foreground, for example, accidentally starting a long-running command, first use the keyboard shortcut Ctrl+Z (press and hold both keys simultaneously) to pause it. This will suspend the job and return control to the shell.

Use bg to move the job to the background:

Once the job is suspended, you can use the bg command to move it to run in the background. The bg command will restart the job and allow it to continue running while you work on other tasks.
To move a suspended job to the background, follow these steps:

Get the job number

Use the jobs command

jobs
# [1] + suspended python insert.py

You can see the suspended task is python insert.py, and the number at the front is the job ID, here it is 1.

Move the job to the background

To move the job with ID 1 to the background, run:

bg %1
# [1] + running python insert.py

The command resumes running from the suspended state.

Use disown to keep the job running after logout:

By default, on Unix-like systems, when you log out, all running jobs associated with your shell session will be terminated. However, you can use the disown command to remove a job from the shell’s job control, handing over its parent process to the init system,
so it continues running after logout.

To remove a job from the shell and keep it running after logout, just use the job number (here, 1), then run:

disown %1

This command has no output, but after running jobs again, you will see the job has disappeared, indicating success.
Once a job is removed from the shell, it will continue running even if you log out.
Note, however, that you will no longer be able to control or monitor the job with the shell’s job control features.

Conclusion:

Managing background jobs on Unix-like systems is crucial for multitasking and improving work efficiency. By first suspending the job with Ctrl+Z, then moving it to the background with bg, and finally separating it from the shell using disown, you can ensure the job keeps running after logout.

Remember to redirect job output if needed, and consider using tools like screen or tmux for more advanced job management.

Related