NOHUP Running the long running commands in background



This article will guide you through how to run the long running command in background without hanging, see the job status and get the output to the file for diagnostic purpose.

We can use nohup and send your long running command to background so that you can continue while the command will keep on executing in the background. After that you can safely log out.

With nohup command we tell the process to ignore the SIGHUP signal which is sent by command on termination, thus making the command persist even after session logout. On session logout the command is detached from the controlling terminal and keeps on running in the background as the daemon process.

Executing command using nohup in the background

Here, is a simple scenario wherein, I am trying to restore a big database file in the background on ssh session using nohup, after which the task was sent to background with prompt returning immediately giving PID and job ID of the process ([JOB-ID] P-ID).

# nohup mysql -uroot -p testdatabasesql.sql < backup.sql &
nohup: ignoring input and appending output to `nohup.out'


To See the list of jobs running in background

# jobs

Resuming the session to view if job is still running

When you re-login again, you can check the status of the command, bring it back to foreground using

# fg %JOB-ID or fg 

To monitor its progress and so on. Below, the output shows that the job was completed as it doesn’t show on re-login, and has given the output which is displayed.

# fg %JOBID

To see the result or any error for restoring the database 

# cat nohup.out


Please give your valuable comments and also share

Keywords: Run command in background, nohup, long run commands

Comments

Popular posts from this blog

Observium: Configuring Microsoft Windows 2008 Server SNMP Agent

AWS: Upgrade PV Drivers on Windows Instances

How to configure Incremental backups for MSSQL Database