1

I've attempted to use the example given here for running a perl script as a daemon:

However, when attempting to start the daemon I'm getting the following error: systemd1: myDaemon.service: Can't open PID file /run/myDaemon.pid (yet?) after start: Operation not permitted

I've found several others who've had this issue, but none of the suggested fixes, such as changing the pid location from /var/run to /run, have worked for me. Any suggestions?

Here is the contents of /etc/init.d/myDaemon:

#!/bin/bash
#
# myDaemon This starts and stops myDaemon 
#
# chkconfig: 2345 12 88
# description: myDaemon is a perl script that does stuff
# processname: myDaemon
# pidfile: /run/myDaemon.pid
### BEGIN INIT INFO
# Provides: $myDaemon
### END INIT INFO

# Source function library.
. /lib/lsb/init-functions

binary="/home/myuser/myDaemon.pl"

[ -x $binary ] || exit 0

RETVAL=0

start() {
    echo -n "Starting myDaemon: "
    daemon $binary
    RETVAL=$?
    PID=$!
    echo
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/myDaemon

    echo $PID > /run/myDaemon.pid
}

stop() {
    echo -n "Shutting down myDaemon: "
    killproc myDaemon
    RETVAL=$?
    echo
    if [ $RETVAL -eq 0 ]; then
        rm -f /var/lock/subsys/myDaemon
        rm -f /run/myDaemon.pid
    fi
}

restart() {
    echo -n "Restarting myDaemon: "
    stop
    sleep 2
    start
}

case "$1" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    status)
        status myDaemon
    ;;
    restart)
        restart
    ;;
    *)
        echo "Usage: $0 {start|stop|status|restart}"
    ;;
esac

exit 0

And here are the relevant contents of the perl script under /home/myUser/myDaemon.pl:

    #! /usr/bin/perl
    use strict;
    use warnings;
    use Proc::Daemon;
    
    # Proc::Daemon
    Proc::Daemon::Init;
    my $continue = 1;
    $SIG{TERM} = sub { $continue = 0 };


while($continue) {
  # does stuff
}
Sandy
  • 66
  • 6
  • Please show the code you used, see [mcve] for more information – Håkon Hægland Apr 03 '21 at 07:03
  • 1
    Does your process have permission to create a file in that directory? Is this a user service? – brian d foy Apr 03 '21 at 09:21
  • The code is exactly as per the example on the other thread, but with the XXXXX replaced with the name of my perl script which is stored in my user's home directory. I'm attempting to start with: #sudo /etc/init.d/myDaemon start – Sandy Apr 03 '21 at 17:09
  • @briandfoy: I'm not sure if the daemon has rights to create the PID. I assume that by starting it with sudo, the daemon itself would have rights, but I'm not sure how this works. – Sandy Apr 03 '21 at 20:08
  • At a glance, with no familiarity with either the tool or the module, I'm thinking using both `daemon $binary` and Proc::Daemon is counterproductive and leading to the issue. Gotta run. Will come back if I remember – ikegami Apr 04 '21 at 00:19

0 Answers0