in a simple startup script I wrote I have:
PID=`su user_name -c "daemon_name > /dev/null 2>&1 & echo \$!"`
echo $PID
The script is run as root. And the echo doesn’t really echo anything 🙂
If I input the exact same command in the shell, it works:
# su user_name -c "daemon_name > /dev/null 2>&1 & echo \$!"
11831
#
Any ideas? Thanks.
EDIT: It was working fine before I tried to run the application as another user.
I was previously using this code:
PID=`daemon_name > /dev/null 2>&1 & echo $!`
echo $PID
and it worked fine as root
daemons are designed to fork multiple times so likely the PID returned from $! will not be the pid you want to work with later. You will probably have to do something like this: