Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find whether a script run as a nohup finished or not?

Tags:

I tried running a script using nohup like,

nohup script.sh & 

When I tried

ps -ef | grep "script.sh" 

I couldn't find it there except for the grep which is being run with that string as a parameter.

Am I doing it right?. Does this mean that the process has indeed finished execution? Thanks.

like image 266
learner135 Avatar asked May 31 '10 13:05

learner135


People also ask

How do I know nohup is finished?

If you see the name of your process/program among those listed then it hasn't finished yet. To check the results or status of the programs, log back in to the same server. Once the job has finished its output will be contained in a file located within your home space. The filename will be "nohup.

How do I know if a script is running in nohup?

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 I get a list of programs running with nohup?

You cannot exactly get a list of commands started with nohup but you can see them along with your other processes by using the command ps x . Commands started with nohup will have a question mark in the TTY column.

How do I know if a script is running in the background?

Open Task Manager and go to Details tab. If a VBScript or JScript is running, the process wscript.exe or cscript.exe would appear in the list. Right-click on the column header and enable "Command Line". This should tell you which script file is being executed.


1 Answers

At the beginning of your shell script, write the PID to a file (for example, in /var/run). Then, you can just search for that PID to know if the process is done or not. You can get the PID of your shell script using the built-in $$ variable.

To record the PID, put at the top of your script:

echo $$ > /var/run/myscript.pid 

Then, to check if it's still running:

ps -p `cat /var/run/myscript.pid` 

You might not be able to write into /var/run as a normal user. If not, just use /tmp

like image 97
Edward Dale Avatar answered Oct 13 '22 20:10

Edward Dale