61

I want to run my script in background and then write its pid file. I am using nohup to do this.

This is what i came up with,

nohup ./myprogram.sh > /dev/null 2>&1 & && echo $! > run.pid 

But this gives a syntax error.

The following doesn't give syntax error but the problem is echo $! doesn't write the correct pid since nohup is run in a sub shell

(nohup ./myprogram.sh > /dev/null 2>&1 &) && echo $! > run.pid 

Any solutions for this, given i want a single line statement for achieving this?

2
  • 4
    The & is a command terminator, much like ; or \n. That is why & && is a syntax error. Commented Nov 28, 2013 at 2:02
  • 2
    I tried this, but it gave me the wrong PID. I wonder what I'm doing wrong? Commented Aug 22, 2014 at 21:54

4 Answers 4

119

You already have one ampersand after the redirect which puts your script in background. Therefore you only need to type the desired command after that ampersand, not prefixed by anything else:

nohup ./myprogram.sh > /dev/null 2>&1 & echo $! > run.pid
7
  • 7
    tried this, but getting a pidfile that's 1 process behind for some odd reason. so if the pidfile says 123, the actual process ID is 124. Any ideas?
    – Allen
    Commented Jun 17, 2016 at 6:25
  • 4
    @Allen, because nohup forks and a new process is created for the actual command. And sometimes that may not be the exact next pid, e.g. during heavy forking of other processes. Commented Dec 7, 2016 at 21:55
  • 1
    @Grigor Yosifov, in the command what is the purpose of: 2>&1 ?
    – Zubair
    Commented Nov 1, 2017 at 15:29
  • @Zubair it's basically suppressing all of the 1st command console messages by pointing both the error and the standard output to the null device. I've left it as it was there by the original request. Commented Nov 4, 2017 at 17:26
  • 4
    nohup ./run.sh > /dev/null & echo $(($!+1)) > run.pid
    – Evhz
    Commented Feb 21, 2018 at 11:32
27

This should work:

nohup ./myprogram.sh > /dev/null 2>&1 &
echo $! > run.pid
3
  • But this uses multiple lines. Can't i do this using single line ? Commented Nov 27, 2013 at 22:30
  • 3
    Yes, the newline is optional. Commented Nov 28, 2013 at 2:00
  • 7
    @amit.codename13: Yes of course you can use it like nohup ./myprogram.sh > /dev/null 2>&1 & echo $! > run.pid I just placed it in 2 lines for understanding purpose only.
    – anubhava
    Commented Nov 28, 2013 at 4:41
4

Grigor's answer is correct, but not complete. Getting the pid directly from the nohup command is not the same as getting the pid of your own process.

running ps -ef:

root     31885 27974  0 12:36 pts/2    00:00:00 sudo nohup ./myprogram.sh
root     31886 31885 25 12:36 pts/2    00:01:39 /path/to/myprogram.sh

To get the pid of your own process, you can use:

nohup ./myprogram.sh > /dev/null 2>&1 & echo $! > run.pid
# allow for a moment to pass
cat run.pid | pgrep -P $!

Note that if you try to run the second command immediately after nohup, the child process will not exist yet.

0

Works with python as well:

nohup python run_experiment.py > experiment.log & echo $! > experiment.nohup.pid

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.