I need to save a pid of a child started with $: su <user> -c “nohup …”

Hello,

I want to save pid of a child process but I get empty file.

su myuser -c "nohup ./mydaemon.sh >/dev/null 2>&1 & print $! > mydaemon.pid"

This one works:

nohup ./mydaemon.sh >/dev/null 2>&1 & print $! > mydaemon.pid

Please help. Thank you in advance.

Try this instead:

su myuser -c 'nohup ./mydaemon.sh >/dev/null 2>&1 & echo $! > mydaemon.pid'

I think $! is being expanded before su is called.

Also ensure mydaemon.pid and . are writable by myuser

That solved it, thank you.