Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use nohup to run process as a background process in linux?

Tags:

linux

I use this command to run my work.

(time bash executeScript 1 input fileOutput $> scrOutput) &> timeUse.txt 

While, 1 is a number of process that I use to run this work. I have to change the number of process for each run. At each time it use long time to complete. Then I want to run it as background process.

How can I do it?

I tried:

nohup ((time bash executeScript 1 input fileOutput $> scrOutput) &> timeUse.txt) 

But it doesn't work.

like image 426
Guilgamos Avatar asked Mar 02 '11 08:03

Guilgamos


People also ask

How do I set a process as a background in Linux?

To bring a process running in the background to the foreground, use the fg command followed by the job id. To put in the background again, press CTRL + Z followed by the bg command.

How does nohup check background process?

Run ping command with nohup command. Re-open the terminal and run pgrep command again. You will get the list of the process with process id which is running. You can stop any background process by running kill command.

How do you make a process run in the background?

In order to place a foreground proces into the background, we must first put the process to sleep, and then place it in the background. Execute the command to run your process. Press CTRL+Z to put the process into sleep. Run the bg command to wake the process and run it in the backround.

What is use of nohup command in Linux?

Nohup, short for no hang up is a command in Linux systems that keep processes running even after exiting the shell or terminal. Nohup prevents the processes or jobs from receiving the SIGHUP (Signal Hang UP) signal. This is a signal that is sent to a process upon closing or exiting the terminal.


2 Answers

In general, I use nohup CMD & to run a nohup background process. However, when the command is in a form that nohup won't accept then I run it through bash -c "...".

For example:

nohup bash -c "(time ./script arg1 arg2 > script.out) &> time_n_err.out" & 

stdout from the script gets written to script.out, while stderr and the output of time goes into time_n_err.out.

So, in your case:

nohup bash -c "(time bash executeScript 1 input fileOutput > scrOutput) &> timeUse.txt" & 
like image 155
Shawn Chin Avatar answered Sep 22 '22 16:09

Shawn Chin


You can write a script and then use nohup ./yourscript & to execute

For example:

vi yourscript 

put

#!/bin/bash script here 

you may also need to change permission to run script on server

chmod u+rwx yourscript 

finally

nohup ./yourscript & 
like image 27
Tong Gao Avatar answered Sep 18 '22 16:09

Tong Gao