Linux – init.d script – Permission denied

bashinitlinux

I'm trying to create a init.d script for my server which should start/stop the teamspeak server and some node.js apps using "pm2". Here's my script:

#! /bin/sh

### BEGIN INIT INFO
# Provides:          my_service
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts/stops all my services
# Description:       starts/stops all my services
### END INIT INFO

echo "running script with argument: $1" >> /log.txt

case "$1" in
  start)
    /usr/local/bin/pm2 resurrect >> /log.txt 2>&1
    /home/teamspeak/ts3server_startscript.sh start >> /log.txt 2>&1
    ;;
  stop)
    /usr/local/bin/pm2 dump >> /log.txt 2>&1
    /usr/local/bin/pm2 delete all >> /log.txt 2>&1
    /home/teamspeak/ts3server_startscript.sh stop >> /log.txt 2>&1
    ;;
esac

echo "done" >> /log.txt

As you can see, the scripts logs the stdout & stderr of every command to /log.txt.

The strange thing is, that when I run /etc/init.d/my_service start/stop manually, it works great. But when I issue a reboot, the 3 pm2 commands fail. Here's the log i get after a reboot of the machine:

running script with argument: stop
exec: 29: : Permission denied
exec: 29: : Permission denied
Stopping the TeamSpeak 3 serverdone
done
running script with argument: start
exec: 29: : Permission denied
Starting the TeamSpeak 3 server
TeamSpeak 3 server started, for details please view the log file
done

Do you have any ideas what this could be? Is this related to pm2, or is it a bug in my script?

What I don't get is, why is it different when script gets executed automatically on poweroff/boot from when I start it manually using the shell.

Best Answer

It looks like your call to pm2 doesn't have sufficient permissions. Try to run it with sudo if you didn't yet to see if it works.

The error itself seems to come from pm2, because your script does not have a line 29.

Usually, on startup init.d runs with a different user with quite a different set of permissions.

Related Question