0

I'm trying to make a specific script load on start on Debian.
I have tried to put the script on /etc/rc.local but no success.
Now I have made a service script that runs my specific script. The code of the service script is:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          script
# Required-Start:    $local_fs $network $named $time $syslog
# Required-Stop:     $local_fs $network $named $time $syslog
# Default-Start:     5
# Default-Stop:      0 1 6
# Short-Description: Start script at boot time
# Description:       Enable service .
### END INIT INFO

case "$1" in
  start)
    echo "Starting script"
    /usr/local/bin/script1
    ;;
  stop)
    echo "Stopping script"
    ;;
  *)
    echo "Usage: /etc/init.d/script {start|stop}"
    exit 1
    ;;
esac

exit 0

Now I can test if my script is run as a service using sudo service <script> start

My code of /usr/local/bin/script1 is:

#!/bin/sh

exec 5> >(logger -t $0)
BASH_XTRACEFD="5"
PS4='$LINENO: '
set -x

FILE=/share/_ngrok
NAO_ESTA_ESCRITO=0

touch $FILE
/usr/local/bin/ngrok tcp 22 --region eu > /dev/null &
sleep 5
URL=`/usr/local/bin/ngrok_url`
grep -q -F "$URL" $FILE || NAO_ESTA_ESCRITO=1
if [ $NAO_ESTA_ESCRITO -eq 1 ]; then
  /usr/local/bin/ngrok_url > $FILE
  /usr/bin/drive push -no-prompt -quiet $FILE
fi
exit 0

When I run /usr/local/bin/script1 from bash it runs successfully but when I run sudo service servicescript start it doesn't run successfully. But when I do sudo service servicescript otherarg it runs successfully, so the service script is well configured. It is just the start that doesn't work on the service.
When I do sudo service servicescript start I get this error:

Job for script.service failed. See 'systemctl status script.service' and 'journalctl -xn' for details.

When I do systemsctl status script.service I get this message:

● script.service - Info
Loaded: loaded (/etc/systemd/system/script.service; enabled)
Active: failed (Result: exit-code) since Qui 2017-03-23 17:19:25 WET; 2min 35s ago
Process: 7710 ExecStop=/usr/local/bin/killscript (code=exited, status=0/SUCCESS)
Process: 11689 ExecStart=/usr/local/bin/script1 (code=exited, status=2)
Main PID: 11689 (code=exited, status=2)

  • If you run systemsctl status script.service as root after the failed start attempt, it prints the logged error messages as well. They should help you diagnose the problem. – Ferenc Wágner Mar 25 '17 at 19:07
0

It looks like you're mixing all three init systems:

  • sysVinit /etc/init.d/daemon start/stop/restart
  • upstart service daemon start
  • systemd systemsctl status script.service

Find out what your init system is

 stat /proc/1/exe    

File: '/proc/1/exe' -> '/lib/systemd/systemd'

So it's systemd which is the latest and new standard.

Here is a link for a howto: How to automatically execute shell script at startup boot on systemd Linux

Remember that noninteractive shells don't have the env set such as PATH, you either need to set the PATH or give the full path for each command such as /bin/sleep.

When using systemd you might want to use journald instead of log files. In that case you'd replace logger with systemd-cat.

  • The service command is designed to do the right thing under all possible init systems, so it's a valid alternative at all times. – Ferenc Wágner Mar 25 '17 at 19:09

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.