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

# 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

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

case "$1" in
    /usr/local/bin/pm2 resurrect >> /log.txt 2>&1
    /home/teamspeak/ts3server_startscript.sh start >> /log.txt 2>&1
    /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

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
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

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.

  • Do you reallyhave #! /bin/sh or do you have #!/bin/sh? The first look for SPACE slash bin slash sh... – Hennes Feb 20 '16 at 16:53
  • Also, please post the output from ls -l on that script so we can check if the x bit is set. – Hennes Feb 20 '16 at 16:54
  • Which is line 29? You should setup also the PATH (you are also working on non standard (for init script) directories. Do you set the executable permissions on the script and on pm2? I assume then in "/etc/init.d/my_service start/stop" you mean space instead of last slash. – Giacomo Catenazzi Feb 20 '16 at 17:07

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.

| improve this answer | |
  • I don't have sudo installed :( Is there another way to run the script as root? – Van Coding May 20 '14 at 7:46
  • Well, then just use the "su" command and run the commands as root that way. – private_meta May 20 '14 at 7:56
  • Won't it ask for the password then? – Van Coding May 20 '14 at 7:58
  • Well of course it does. You can only run commands as root if you either have the password or if root set it up so you get root permissions through sudo. As you don't have the latter, you need the root password to run applications as root. If you don't have those, you need to look for other options. Check your permissions for pm, or maybe you just can't write a logfile to the root folder (try to write the log to a folder you have permissions to). – private_meta May 20 '14 at 8:01
  • I have the password, but I don't want my server to be able to restart without me typing in the password everytime. So the only solution is installing sudo? – Van Coding May 20 '14 at 8:03

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.