Init Script Not Running On System Reboot/Shutdown - linux

I have made a bash init script in file at /etc/init.d/redis-snapshot:
#!/bin/bash
### BEGIN INIT INFO
# Provides: redis-snapshot
# Required-Start: $local_fs $syslog Stime redis-server
# Required-Stop: $local_fs $syslog $time redis-server
# Default-Start: 0 6
# Default-Stop: 2 3 4 5
# Short-Description: Backup redis data on system exit
# Description: redis-snapshot is a simple bash script to dump redis data
# to disk whenever the system shuts down or reboots.
### END INIT INFO
# path to script log file
ACTIVITYLOG='/home/noman/Desktop/redis-snapshot.log'
# create/touch log file
touch $ACTIVITYLOG
# invoke the save command on redis-cli
# this will dump all in-memory data to disk
OUTPUT="$(redis-cli SAVE)"
echo -e "$(date +'%Y-%m-%d %H:%M:%S %Z')\tSnapshoting Redis Data To Disk" >> $ACTIVITYLOG
echo -e "$(date +'%Y-%m-%d %H:%M:%S %Z')\tRedis says $OUTPUT" >> $ACTIVITYLOG
# end - nothing to do
I am using the following command to make it run on system reboot and shutdown:
sudo update-rc.d redis-snapshot start 99 0 6 .
It does not work for some reason and I am clueless what to try next.
Any bright ideas?

What is your underlying distro and release?
Try
chkconfig --add redis-snapshot

If you wish to specifically find out whether your script is run, you may try skipping output redirection to file, so you'd see it during boot-up or checking /var/log/boot.log after boot.
If the output is missing, then your script was not added to /etc/rc#.d.
An alternative method of running a script on startup (which I prefer) is via /etc/rc.local. E.g.:
bash /root/script.sh
exit 0

Related

Starting vnc-server with command every startup in linux

I run a headless Kali Linux raspberry pi.
I got the ssh to work properly and start at boot.
I got the vnc working but only when I start it manually from terminal.
Problem:
I need to start the vnc-server every time the raspberry boots.
And I have tried like a 100 different things from google but this seems to be the "most" successful for me.
I have created a .sh file with this content:
File path: /root/Documents/startavnc.sh
x11vnc -gui -display :0 -usepw -rfbport 5900 -bg
This code works when I run it directly in terminal.
I have then created this file:
/etc/init.d/startavnc
With the following contents:
#!/bin/sh
### BEGIN INIT INFO
# Provides: startavnc
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Should-Start: $network $time
# Should-Stop: $network $time
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start and stops the vnc-server
# Description: Controls the automatic start of vnc-server at reboot
### END INIT INFO
/root/Documents/startavnc.sh
I took these LBG-stuff from another file and I'm not sure if I need all. But I got some errors when they where not included so I added them.
I then ran:
chmod ugo+x /etc/init.d/startavnc
update-rc.d startavnc defaults
When it didn't work I googled some more and ran:
ln -s /etc/init.d/startavnc /etc/rc.d
And it still aren't executing when I boot up the pi.
Can you see some obvious fault in this?
I'm, as you probably already figured, a Linux newbie...
Thanks!

Won't boot because of rc.local start up script

Environment :
Virtualization software : VMware Workstation 12 Player
Guest Machine : Red Hat 6.4
Host Machine : Windows 7 Professional Service Pack 1
Question :
I'm trying to execute automatically a svn update at the boot of my virtual machine. I edited the rc.local file like this :
#!/bin/sh -e
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
sleep 10
svn update path/to/repository
exit 0
But with this the virtual machine won't boot, it keeps loading (If I remove the svn command line, there is no problem). And if I try to shutdown or restart the guest, we can see this :
So it means that svn command was executed but it didn't work. I already tried writing "svn update --username user --password xxx" but same result. How do I get the svn command to run?
What if you create the file in /etc/init.d/svnserve
#!/bin/bash
### BEGIN INIT INFO
# Provides: svnserve
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# X-Interactive: true
# Short-Description: Start/stop svnserve
### END INIT INFO
svnserve -d -r /PATH/TO/YOUR/REPOSITORY
Make it Executable:
chmod u+x /etc/init.d/svnserve
Create links from runlevels to the script:
update-rc.d svnserve
Then check if the links were created correctly
find /etc/ -name '*svnserve*'
Best regards
Xsi

Unable to use service command with debian 8 (Jessie)

In order to install LibreOffice 4.4 into my Debian 8 (Jessie), I just got all my bash scripts from my Debian 7.5 and run them into the same way into the D8 one.
I know there was several changes into the new version but I'm not able to use my service like this anymore :
sudo service libreoffice start
When doing this doesn't start anything and I have to start it using :
sudo /etc/init.d/libreoffice start
And strange thing, when doing (bad parameter) :
sudo service libreoffice dzedjiodjzedj
...the script is perfectly executed and it displays my catched error
Here is my /etc/init.d/libreoffice file :
#
# libreoffice This shell script takes care of starting and stopping the LibreOffice Daemon
#
# chkconfig: - 80 20
#
### BEGIN INIT INFO
# Provides: libreofficedaemon
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Init.d script to run a LibreOffice Daemon
# Short-Description: start and stop LibreOffice Daemon
### END INIT INFO
NAME="LibreOffice Service"
LIBREOFFICE_HOME=/opt/libreoffice4.4
LIBREOFFICE_USER=libreoffice
export LIBREOFFICE_HOME LIBREOFFICE_USER
start() {
echo -ne "Starting $NAME. \n"
su $LIBREOFFICE_USER -c "$LIBREOFFICE_HOME/start.sh"
}
stop() {
echo -ne "Stopping $NAME. \n"
su $LIBREOFFICE_USER -c "$LIBREOFFICE_HOME/stop.sh"
}
kill() {
echo -ne "Force close of $NAME. "
killall -u $LIBREOFFICE_USER
}
cd $LIBREOFFICE_HOME
case "$1" in
start|stop)
$1;;
restart) stop; start;;
kill) kill;;
*)
echo "Usage: /etc/init.d/libreoffice {start|stop|restart|kill}"
exit 1
;;
esac
exit 0
And I just run that issue with tomcat8 service yesterday, I just started manually the service and sudo service tomcat8 start worked after that but nothing for libreoffice one..
From the Debian Jessie Release Notes :
When you are asked if any file in the /etc/init.d directory, or the /etc/manpath.config file should be replaced by the package maintainer's version, it's usually necessary to answer “yes” to ensure system consistency
With systemd you now have to use systemctl:
sudo systemctl start libreoffice
Here's some more info

How to start a Node.js app on system boot?

I'm working on a Raspberry Pi running Raspbian running a Node.js app and trying to get it to start when the Pi boots. I found a couple of examples but I can't seem to get it working. My current code is:
#! /bin/sh
# /etc/init.d/MyApp
### BEGIN INIT INFO
# Provides: MyApp.js
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starts MyApp.js
# Description: Start / stop MyApp.js at boot / shutdown.
### END INIT INFO
# If you want a command to always run, put it here
# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting MyApp.js"
# run application you want to start
node /home/pi/app/MyApp/MyApp.js
;;
stop)
echo "Stopping MyApp.js"
# kill application you want to stop
killall MyApp.js
;;
*)
echo "Usage: /etc/init.d/MyApp {start|stop}"
exit 1
;;
esac
exit 0
I have this in the etc/init.d folder, ran chmod +x /etc/init.d/MyApp, I'm able to run it manually, then I run sudo update-rc.d MyApp defaults, reboot and the script never runs. I've looked at some different examples, made adjustments and still no luck.
I solved this problem by first checking where node.js was installed on RaspberryPi:
which node
This gave me :
/usr/local/bin/node
Open crontab config:
sudo crontab -e
Then in my crontab :
#reboot sudo /usr/local/bin/node <complete path to your .js app> &
Save, reboot, and problem solved !
Mohit is right, but just for clarification, you can use readlink to find the full path for your Node.js app as it will be needed later to add as a cron job.
readlink -f <<name of file >>
For instance readlink -f HAP-NodeJS/Core.js results in /home/pi/HAP-NodeJS/Core.js
You can also use which node to find the full path where node.js is installed
Next, create a new cron job using sudo crontab -e and add the following code at the very end:
#reboot sudo /usr/local/bin/node <<.js application path>> &
for instance, my code looks like this.
#reboot sudo /usr/local/bin/node /home/pi/HAP-NodeJS/Core.js &
Upon reboot (or start up) , your Node.js should run. Hope this clears things.
If you're using a prebuilt Pi release like 0.10.24, you may be experiencing a PATH issue.
You can either provide the full path to the node binary as part of the start command or make sure the PATH to the node binaries are set before /etc/init.d/MyApp is ran. I had the same issue and tried both with success. Also, the stop command as you have it may not be working.
#! /bin/sh
# /etc/init.d/test
### BEGIN INIT INFO
# Provides: test
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Example initscript
# Description: This file should be used to construct scripts to be
# placed in /etc/init.d.
### END INIT INFO
# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting test.js"
# run application you want to start
#node /home/pi/test.js > /home/pi/test.log
/home/pi/downloads/node-v0.10.24-linux-arm-pi/bin/node /home/pi/test.js >> /home/pi/test.log
;;
stop)
echo "Stopping test.js"
# kill application you want to stop
killall -9 node
# Not a great approach for running
# multiple node instances
;;
*)
echo "Usage: /etc/init.d/test {start|stop}"
exit 1
;;
esac
exit 0
If you'd like to do sudo node, you can add the PATH to Defaults secure_path using sudo visudo.
Also, I would recommend using something like forever to keep your process running after crashes and what not.

Starting service automatically failed

I'm currently facing an strange issue.
I'm using debian squeeze under linux voyage on an alix.2d2 and I'm tring to launch an home made script at boot using init.d.
So to do that, I'm writing a simple script, putting it into /etc/init.d/ (/etc/init.d/linknx) and using update-rc.d configuring the boot.
update-rc.d linknx start 191 12345
Before rebooting, I'm testing the script using :
service linknx start
and it works nice.
When system reboots, script is not launched. I'm trying to change the boot config using update-rc.d
update-rc.d linknx defaults
Same issue.
So After that, I'm cleaning the boot config, and add two lines in the /etc/rc.local.
> sh -c "logger -p local0.notice [LAUNCHTEST] "rc.local invoking"
> service linknx start
The first line pass, but the second one fail.
Somebody can identify the problem ?
Thanks for your help !
regards
The script:
#!/bin/sh -e1
### BEGIN INIT INFO
# Provides: linknx
# Required-Start: $local_fs $remote_fs $network $syslog $nocatsplash
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# X-Interactive: false
# Short-Description: Start/stop linknx daemon
# Description: Simnet linknx daemond starter.
### END INIT INFO
now=$(date +"%F %k:%M:%S")
port=3671
ip=192.168.2.10
LD_LIBRARY_PATH=/usr/local/lib
knx_config_file=/etc/linknx.xml
knx_write_file=/etc/linknx.xml
log_tag="[LINKNX]"
log_level="-p local0.notice"
case "$1" in
start)
logger $log_level -t $log_tag -s "Starting linknx and eibd ..."
ldconfig -l
eibd -d -D -S -T -i ipt:$ip:$port
linknx -d --config=$knx_config_file --write=$knx_write_file
logger $log_level -t $log_tag -s "Done\n"
;;
stop)
;;
reload|restart|force-reload)
;;
test)
logger $log_level -t $log_tag -s "[$now] - [TEST] - This is a simple test to check behavior of the program ..."
;;
*)
logger $log_level -t $log_tag -s "[$now] - [FATAL] - Unknow command, only available are start|stop|restart|reload|force-reload"
;;
esac
exit 0
You should be using
insserv linknx
And not
update-rc.d linknx defaults
As of Debian 6.0 (Squeeze), update-rc.d has been replaced with insserv (see here). Why? Because 6.0 introduced a new boot process, for which you have the headers in place (the INIT INFO section at the top of the script).

Resources