I have a question concerning a init.d script.
Currently I have a init script that launches a custom daemon as follows:

sudo -u "$user" $cmd >> "$stdout_log" 2>> "$stderr_log" &
echo $! > "$pid_file"

Daemon works as long as I run the init script as root.
The daemon needs access to create screens, but because of CentOS 6.7 I need to use a workaround to allow non-root users to run screens by using the command ´script´ (How can I start a 'screen' session as non-root user) and I end up with the following line that starts the daemon perfectly as a non-root user.

sudo -u "$user" bash -c "script -c \"$cmd\"" >> "$stdout_log" 2>> "$stderr_log" &

Issues begin when I try to echo and pipe the pid of the process to the pid file as it gives the pid of the ´script´ command and not of the daemon itself.

Does anyone have a suggestion or a solution for getting the pid of the daemon?

1 Answer 1


How about

sudo -u "$user" bash -c "script -c \"$cmd& echo $! > \"$pid_file\"\"" >> "$stdout_log" 2>> "$stderr_log" &

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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