When working with legacy systems like CentOS 6.4, you might encounter the frustrating "daemon: command not found" error while trying to run processes under different users. Let's dive into why this happens and how to properly implement service daemonization.
The daemon
function is actually a shell function defined in /etc/init.d/functions
, not a standalone executable. The error occurs because:
1. The script isn't properly sourcing the functions file
2. The function might not be available in your shell environment
3. There could be permission issues with the functions file
Here's how your init script should be structured:
#!/bin/bash
#
# mydaemon Start/Stop my custom daemon
#
# chkconfig: 345 20 80
# description: My custom service
# Source function library
. /etc/init.d/functions
start() {
echo -n "Starting mydaemon: "
daemon --user=someuser --pidfile=/var/run/mydaemon.pid \
/path/to/mydaemon >/dev/null 2>&1 &
RETVAL=$?
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/mydaemon
echo
return $RETVAL
}
If sourcing the functions file doesn't work, consider these alternatives:
Using start-stop-daemon
start-stop-daemon --start --quiet --background \
--chuid someuser --exec /path/to/mydaemon \
--pidfile /var/run/mydaemon.pid
Manual Daemonization
start() {
echo -n "Starting mydaemon: "
nohup sudo -u someuser /path/to/mydaemon >/dev/null 2>&1 &
echo $! > /var/run/mydaemon.pid
echo "OK"
}
When troubleshooting:
- Verify the functions file exists: ls -l /etc/init.d/functions
- Check file permissions: should be 644 root:root
- Test sourcing manually: . /etc/init.d/functions && type daemon
- Examine environment variables: env | grep PATH
- Verify the user exists: id someuser
Remember that CentOS 6.4 is quite old (released in 2013). For modern systems, consider:
- Upgrading to CentOS 7+ which uses systemd
- Using modern process managers like supervisord
- Containerizing the application with Docker
When working with init scripts on CentOS 6.4, you might encounter the frustrating "daemon: command not found" error, especially when trying to daemonize processes under different users. This commonly occurs when the system can't properly source the necessary functions.
The daemon()
function is actually declared in /etc/init.d/functions
, but your script might not be loading it properly. Here's what's likely missing:
# Make sure this line exists at the top of your init script
. /etc/init.d/functions
Here's a corrected version of your service script:
#!/bin/sh
#
# mydaemon Start/Stop the mydaemon service
#
# chkconfig: 2345 90 10
# description: My custom daemon service
. /etc/init.d/functions
start() {
echo -n "Starting mydaemon: "
daemon --user someuser --name mydaemon /path/to/mydaemon
RETVAL=$?
echo
return $RETVAL
}
stop() {
echo -n "Shutting down mydaemon: "
killproc mydaemon
RETVAL=$?
echo
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit $RETVAL
If you're still having issues, consider these alternatives:
1. Using start-stop-daemon:
start-stop-daemon --start --quiet --pidfile /var/run/mydaemon.pid \
--chuid someuser --exec /path/to/mydaemon
2. Direct nohup approach:
su - someuser -c "nohup /path/to/mydaemon > /dev/null 2>&1 &"
- Verify the script has execute permissions:
chmod +x /etc/init.d/mydaemon
- Check that the someuser account exists
- Ensure SELinux isn't blocking the operation
- Test with absolute paths for all commands