Posted: . At: 10:10 AM. This was 2 years ago. Post ID: 8101
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.

How to leave a command running when your SSH session is disconnected. Use the nohup command.

Use the nohup command to leave a command running if you are disconnected from the shell

If you want to leave a command running if you are disconnected from your SSH session, then use the nohup command, this can be very handy. This is a good example, this will run the Nmap scan and then log out the user. When you re-connect, it will still be running.

nohup sudo nmap -A -T2 -P0 hackthissite.com -oN nmap-log.log &exit

So, this is a good way to run a command over SSH that will take a long time. The -oN nmap-log.log parameter to Nmap will output the scan results into a file named nmap-log.log for later perusal when you log back in. Otherwise, the penetration tester would have to wait for ages whilst a long Nmap scan is completed. This is a very useful Linux tip and well worth knowing when the user is accessing a machine like Amazon AWS over an Internet connection and the connection is not always reliable.

Very old CRT terminal.
Very old CRT terminal.

This way, the user can run commands over a flaky Internet connection, and the command will still be running when the user is disconnected. This is why the Linux command line is so powerful.

This is another example of the usage of this command.

jason@jason-Lenovo-H50-55:~$ nohup ./arma3server_x64 -name=Afghanistan -mod="@cba_a3;@rhsafrf;@rhsgref;@rhsusaf;@project opfor;@zeus enhanced" -autoInit -config=server.cfg &

Just run the command and press ENTER twice to get back to the terminal.

I ran this command and the PID was printed to the terminal, this is what I can use to kill the process later if I wish.

jason@jason-Lenovo-H50-55:~/.steam/steamcmd/arma3$ nohup ./arma3server_x64 -name=Afghanistan -mod="@cba_a3;@rhsafrf;@rhsgref;@rhsusaf;@project opfor;@zeus enhanced" -autoInit -config=server.cfg &
[1] 1301

Then it is easy to kill the process like this.

jason@jason-Lenovo-H50-55:~/.steam/steamcmd/arma3$ kill 1301

Another way I have found is to run the command like this.

jason@jason-Lenovo-H50-55:~$ ./arma3server_x64 -name=Afghanistan -mod="@cba_a3;@rhsafrf;@rhsgref;@rhsusaf;@project opfor;@zeus enhanced" -autoInit -config=server.cfg &!

The $! at the end will allow the process to run in the background. I exited the SSH terminal I was using and then logged back in and the process was still running. But it prints output to the current terminal you ran it from. Better to use redirection or to use the nohup example instead. But I tested this with the bash shell, other shells might not work. This is a possible thing to try if you do not have nohup for some reason.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.