Running in the Background

How to run and manage long-running commands in the background so you can log off the server.

Commands for using Screen

Start a screen

screen -S screenName

Detach screen

Ctrl+a d

List screens

screen -ls

Reattach screen

screen -r [id]

Using nohup

Simple use

nohup <command> <args> &

  • Will log to nohup.out

If you need to use sudo

sudo -b <command> <args>

With log file

nohup <command> <args> >> /media/SPE/Path/LogFile.log 2>&1 &

Examining commands in the background

  • When you run a command with nohup you will see the PID or process ID:

[1] 8044

  • To list running processes: ps aux

  • To search running processes: ps aux | grep mycommand

  • You an also use the shortcut function: check mycommand

$ ps aux | grep rake gw234478 5170 3.6 26.3 2379904 2114272 pts/0 Sl+ 13:00 6:26 ruby /usr/local/rvm/gems/ruby-2.4.1@espyMetadata/bin/rake export:hyrax gw234478 19203 0.0 0.0 112712 968 pts/1 R+ 15:56 0:00 grep --color=auto rake

  • The last line (PID 19203) will usually be the grep process itself

Killing background commands

  • if you need to stop the process (not recommended) use this command:

  • sudo kill -9 <PID>

Related articles