how to check whether Linux is going down for halt or reboot - linux

I need to execute some command when system is going down for halt/poweroff.
I need to know this inside my script /etc/init.d/myScript which is called when system is going down.
I used to do it by checking $runlevel variable and if it is set to 0, then system was going down for halt, it was working for instance in Raspbian a few years ago.
However, in my Linux cubieboard2 4.9.12-sunxi Armbian this variable is not set, and when I execute "runlevel" command inside "STOP" case in myScript it says it is in N 5 runlevel which is not true because I have executed poweroff
All tips are welcome.

Handling runlevel in script will not be needed if you are adhering to LSB standard.
As per LSB spec, you might like to header to your script. Following is a example
header.
### BEGIN INIT INFO
# Provides: scriptname
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
More details at chapter 20 of LSB standard
Additionally, you can control runlevel at which your script will invoked.It can also be controlled by chkconfig command

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

How to run a program on startup in Debian

I am attempting to run a program that I made on startup on my Raberry Pi 2. It is running Debian (Jessie) but it doesn't seem to work. I tried following this guide: Debian: Running Scripts on StartUp and creating a system link but nothing seems to be working. The program works when I run it not on startup. Here's the code:
#!/bin/sh
#/etc/init.d/StartGRIP.sh
#
### BEGIN INIT INFO
# Provides: StartGRIP.sh
# Required-Start: $local_fs $network
# Required-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starts GRIP Pipeline
# Description: During startup, runs below command and starts GRIP
### END INIT INFO
# Start processing
env LD_LIBRARY_PATH=/home/pi/vision/grip:LD_LIBRARY_PATH java -jar...
The last command works so I didn't include the whole line as it is a long line.
Any help is greatly appreciated!
Create a desktop entry for the program and place it in ~/.config/autostart
As an example that starts Safe Eyes program placing it as ~/.config/autostart/safeeyes.desktop.
[Desktop Entry]
Encoding=UTF-8
Version=0.9.4
Type=Application
Name=Safe Eyes
Comment=Protect your eyes from eye strain
Exec=safeeyes
OnlyShowIn=XFCE;
StartupNotify=false
Terminal=false
Hidden=false
Categories=Utility;
[Optional] Set the proper permission.
sudo chmod 644 /.config/autostart/[program-name].desktop
Alternative way:
sudo crontab -e
And add
#reboot file_you_want_to_run &

Why is a deluge startup script necessary?

I'm currently working on making a spare Raspberry Pi into a headless BitTorrent box, using Deluge.
Most guides on setting up Deluge on Linux include a custom startup script that will be run at boot. However, when you're SSH'd into the Pi, you can start the deluged daemon by simply typing in "deluged".
However, when I wrote a basic bash script which ran this command, put it in /etc/init.d/ and added it using update-rc.d, it didn't work.
nano /etc/init.d/startdeluged.sh
chmod 755 /etc/init.d/startdeluged.sh
update-rc.d startdeluged.sh defaults
The bash script contained this:
#!/bin/sh
deluged
exit
I'm new to setting up startup scripts on Linux, and I'm just wondering why a special script is necessary when simply running a command in the Terminal has the same effect. Does it have anything to do with the user who is entering the command?
You must add the begin init infos after #!/bin/sh. It looks like that :
### BEGIN INIT INFO
# Provides: scriptname
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
More infos there : https://wiki.debian.org/LSBInitScripts
The script at this page may interest you : http://dev.deluge-torrent.org/wiki/UserGuide/Service/DebianUbuntuInitd

debian redis update-rc.d errors - there is a loop between redis and nginx

tried to install redis as a service in debian 7 wheezy(redis version 7) by following the instructions from http://redis.io/topics/quickstart
but after running sudo update-rc.d redis_6379 defaults
i get these errors
update-rc.d: using dependency based boot sequencing
insserv: warning: script 'redis_6379' missing LSB tags and overrides
insserv: There is a loop between service nginx and redis_6379 if stopped
insserv: loop involving service redis_6379 at depth 2
insserv: loop involving service nginx at depth 1
insserv: Stopping redis_6379 depends on nginx and therefore on system facility `$all' which can not be true!
insserv: exiting now without changing boot order!
update-rc.d: error: insserv rejected the script header
Nginx init.d is
#! /bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/opt/nginx/sbin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/opt/nginx/sbin/nginx
NAME=nginx
DESC=nginx
test -x $DAEMON || exit 0
# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
. /etc/default/nginx
fi
set -e
Redis init.d is
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"
Try to add header to /etc/init.d/redis_6379
### BEGIN INIT INFO
# Provides: redis_6379
# Required-Start: $network $remote_fs $local_fs
# Required-Stop: $network $remote_fs $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop redis_6379
# Description: Redis daemon
### END INIT INFO
and run
sudo update-rc.d redis_6379 defaults

Resources