0

I am trying to execute one script at startup on ubuntu 16.04 LTS desktop. I have added my script to /etc/init.d location. But after restart I am unable to see my script running using

pidof -s pgd

Once I execute this command sudo service run_pgd start it started with a pid. I want this script to be executed in the background.

I have achieved the same thing by adding the script in rc.local it was working fine. But I am having issue in /etc/init.d location This is the script file

#!/bin/bash

###
# Configuration section
# 

# Specify full path to directory with pgd executable and config files here:
# For example: PGD_DIR="/usr/local/Neurotechnology/Activation"
PGD_DIR="/opt/Neurotec_Biometric_10_0_SDK/Bin/Linux_x86_64/Activation"

#
# End of Configuration section
###


# If PGD_DIR is not set use the directory of current script
if [ ! "${PGD_DIR}" ]
then
    PGD_DIR=`dirname "$0"`

    # If we were called through relative path, make absolute one
    if [ "${PGD_DIR:0:1}" != "/" ]
    then
        PGD_DIR="$PWD/$PGD_DIR"
    fi
fi

NAME=pgd
PROGRAM="${PGD_DIR}/${NAME}"
if [ "`uname -s`" = "Darwin" ]
then
    LOG=/Library/Logs/pgd.log
else
    LOG=/tmp/pgd.log
fi

###
# Common routines section
#

echo_()
{
    echo "run_pgd.sh:" "$*"
}

get_pid()
{
    if which pidof &> /dev/null
    then
        echo `pidof -s $NAME`
    else
        ps axc|awk "{if (\$5==\"$NAME\") print \$1}"
    fi
}

start_pgd()
{
    echo_ "starting $NAME..."
    PRESERVE_DIR="$PWD"
    cd "$PGD_DIR"
    "$PROGRAM"
    cd "$PRESERVE_DIR"
    sleep 1
    if [ -s "$LOG" ]
    then
        echo_ "$NAME run log ($LOG):"
        echo  "----------------------------------------"
        cat   "$LOG"
        echo  "----------------------------------------"
    fi
}

stop_pgd()
{
    PID=`get_pid`
    if [ $PID ]
    then 
        echo_ "stopping $NAME..."
        kill $PID
        echo_ "$NAME (pid=$PID) was sent a signal."
    else
        echo_ "$NAME processs not running!"
        exit 1
    fi
}

###
# Body...
#

if ! test -d "$PGD_DIR"
then
    echo_ "Wrong PGD_DIR variable value specified: $PGD_DIR"
    echo_ "Please point it to correct place with $NAME application."
    echo_ "Please also ensure that needed configuration files are there."
    exit 1
fi 
if ! test -f "$PROGRAM"
then
    echo_ "$PROGRAM application not found!"
    exit 1
fi
if ! test -x "$PROGRAM"
then
    echo_ "$PROGRAM application not runable!"
    exit 1
fi


case "$1" in
  start)
    PID=`get_pid`
    if [ $PID ]
    then
        echo_ "$NAME is already running (pid: $PID)"
        exit 1
    fi

    [ ! -f $LOG ] || rm $LOG
    start_pgd
    ;;
  stop)
    stop_pgd
    ;;
  restart)
    stop_pgd

    sleep 4

    if [ `get_pid` ]
    then
        echo_ "$NAME has not stopped!"
        echo_ "restart failed."
        exit 1
    fi

    [ ! -f $LOG ] || rm $LOG
    start_pgd
    ;;
  log)
    PID=`get_pid`
    if [ $PID ]
    then 
        echo_ "$NAME is running with pid: $PID"
    else
        echo_ "$NAME processs not running!"
    fi

    if [ -s "$LOG" ]
    then 
        echo_ "log ($LOG):"
        echo  "----------------------------------------"
        cat   "$LOG"
        echo  "----------------------------------------"
    else
        echo_ "log file (\"$LOG\") is empty."
    fi
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|log}"
    exit 1
esac

exit 0

Is there something wrong with my script ? What are the required changes do I need to make to execute this script at startup ? I want this script to be executed even if the system reboots after crash or any failure.

Regards

Lokesh Pandey
  • 101
  • 1
  • 4

3 Answers3

2

Scripts in /etc/init.d are not executed at startup by default. This is just the location where startup scripts are (or, by now, were) located.

After placing your script there you need to create symlinks in /etc/rc[1-6].d. You can do that manually, or by running:

update-rc.d <scriptname> enable

Note: In Ubuntu 16.04 the old init scripts are deprecated. You should create a systemd service definition for your script.

Gerald Schneider
  • 19,757
  • 8
  • 52
  • 79
  • should I directly give the script file name from /etc/init.d to enable it to execute at startup ? – Lokesh Pandey Nov 22 '17 at 07:16
  • yes, just the filename – Gerald Schneider Nov 22 '17 at 07:17
  • It says `update-rc.d: error: run_pgd.sh Default-Start contains no runlevels, aborting.` – Lokesh Pandey Nov 22 '17 at 07:18
  • Well, then make sure your script is written according to the standards. But personally, I wouldn't waste any time doing this for the old upstart standard, you are going to have to do it all over again when you update to an Ubuntu version where only systemd is used. better to do it directly the systemd way. – Gerald Schneider Nov 22 '17 at 07:20
  • According to [this answer](https://serverfault.com/a/758894/38644) you need to run `update-rc.d run_pgd.sh defaults` before you enable it. I can't try it myself right now. – Gerald Schneider Nov 22 '17 at 07:23
1

You can add your script to crontab with @reboot: @reboot /path/to/my/script.sh

To complete my answer, i suggest your to use the following shebang instead of the actual one to properly load bash env with correct paths etc. : #! /usr/bin/env bash.

o_be_one
  • 11
  • 3
1

init scripts are deprecated as of Ubuntu 16.04. You should create a systemd service to achieve what you want.

Give it a name like myservice.service and place it in /lib/systemd/system/, then enable it with systemctl enable myservice.service

Steve Stoveld
  • 140
  • 3
  • 9