Unable to start a script at startup ubuntu
Solution 1
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.
Solution 2
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
Solution 3
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
.
Related videos on Youtube
Lokesh Pandey
SOReadyToHelp I am a software developer, currently I am working as a Java developer in India. I love to solve complex problems and always try to learn new things. As a programmer I strictly follow the coding conventions.I am a frequent code writer on hacker rank. And always love to spend time on Stack Overflow. Technical Skills : C and C++ developer since high school, Java (Core and Advance), JavaScript, Php, Android development, MySQL and PostgreSQL, Firebase Framework worked on : Angular Springboot (In progress :) ) Area of Interest Cloud computing Algorithm optimization Dynamic multi-threaded programming Mathematics
Updated on September 18, 2022Comments
-
Lokesh Pandey over 1 year
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 usingpidof -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 over 6 yearsshould I directly give the script file name from /etc/init.d to enable it to execute at startup ?
-
dortegaoh over 6 yearsyes, just the filename
-
Lokesh Pandey over 6 yearsIt says
update-rc.d: error: run_pgd.sh Default-Start contains no runlevels, aborting.
-
dortegaoh over 6 yearsWell, 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.
-
dortegaoh over 6 yearsAccording to this answer you need to run
update-rc.d run_pgd.sh defaults
before you enable it. I can't try it myself right now. -
Lokesh Pandey over 6 yearsThank you steve. Yea I have move on to systemd services. +1.