init.d script - Permission denied

14,555

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.

Share:
14,555

Related videos on Youtube

Van Coding
Author by

Van Coding

Updated on September 18, 2022

Comments

  • Van Coding
    Van Coding over 1 year

    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.

    • ganesh
      ganesh about 8 years
      Do you reallyhave #! /bin/sh or do you have #!/bin/sh? The first look for SPACE slash bin slash sh...
    • ganesh
      ganesh about 8 years
      Also, please post the output from ls -l on that script so we can check if the x bit is set.
    • Giacomo Catenazzi
      Giacomo Catenazzi about 8 years
      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.
  • Van Coding
    Van Coding almost 10 years
    I don't have sudo installed :( Is there another way to run the script as root?
  • private_meta
    private_meta almost 10 years
    Well, then just use the "su" command and run the commands as root that way.
  • Van Coding
    Van Coding almost 10 years
    Won't it ask for the password then?
  • private_meta
    private_meta almost 10 years
    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).
  • Van Coding
    Van Coding almost 10 years
    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?
  • private_meta
    private_meta almost 10 years
    Well, you should actually exhaust the other options. Run all the commands by hand and check the result, if it shows an error, try to analyze why it shows the error. One error might be you don't have sufficient permissions for executing pm2, the other error might be you can't write files to /, try to check for both first, and try to think of other things. If you can't think of anything else, you might install sudo. That being said, just using sudo doesn't mean you can restart without a password, it means you can restart using YOUR password instead of ROOTs password.
  • Van Coding
    Van Coding almost 10 years
    Actually, I'm root so I have all the permissions I want. I of course can write to the / directory, otherwise my logfile would not have been created. I also can run the pm2 command withouth problem, if i run it from the shell. It only does not work when the script gets executed from the init system. Do you know what user starts init scripts?
  • private_meta
    private_meta almost 10 years
    Sorry, I don't know that, you can use whoami in a script when booting and log the result to a folder if you want to.
  • Van Coding
    Van Coding almost 10 years
    whoami says it's root :S That's a really weird thing. Maybe I should ask the devs of pm2 for help.. Thank you!!