shell script before splash screen in pi - linux

I have a raspberry pi running the Jessie OS, and would like to run a bash script to execute before it does anything else, such as the splash screen.
The shell script is a simple feh command because I want the pi to operate as a slideshow of images in a picture frame and nothing else; hence needing the pi to start the shell script immediately.
It would also be great if I could have the functionality to restart it if the shell script is exited for any reason.
I have tried cron jobs, /etc/init.d, and /etc/rc0.d, however, none of these have worked for me yet.
I have my main shell script in /home/imgs/slideshow.sh and then I run the following script to set things up to hopefully run as init.d:
#!/bin/bash
sudo apt-get update
sudo apt-get install feh
sudo apt-get install xscreensaver
sudo apt-get upgrade
# Make slideshow startup file in init.d if it doesn't exit
# if it does, make a backup file and rewrite it
FILE=/etc/init.d/slideshow.sh
if [ -f "$FILE" ]
then
sudo mv $FILE $FILE-backup-$(date +"%F-%T")
else
echo "$FILE does not exist but will be created"
fi
# input this text into the file
/bin/cat <<EOM >"$FILE"
#!/bin/sh -e
DAEMON="slideshow" #Command to run
daemon_OPT="" #arguments for your program
DAEMONUSER="user" #Program user
daemon_NAME="slideshow" #Program name (need to be identical to the executable).
PATH="/sbin:/bin:/usr/sbin:/usr/bin" #don't touch
test -x $DAEMON || exit 0
. /lib/lsb/init-functions
d_start () {
log_daemon_msg "Starting system $daemon_NAME Daemon"
start-stop-daemon --background --name $daemon_NAME --start --quiet --chuid $DAEMONUSER --exec $DAEMON -- $daemon_OPT
log_end_msg $?
}
d_stop () {
log_daemon_msg "Stopping system $daemon_NAME Daemon"
start-stop-daemon --name $daemon_NAME --stop --retry 5 --quiet --name $daemon_NAME
log_end_msg $?
}
case "$1" in
start|stop)
d_${1}
;;
restart|reload|force-reload)
d_stop
d_start
;;
force-stop)
d_stop
killall -q $daemon_NAME || true #replace with an apropriate killing method
sleep 2
killall -q -9 $daemon_NAME || true #replace with an apropriate killing method
;;
status)
status_of_proc "$daemon_NAME" "$DAEMON" "system-wide $daemon_NAME" && exit 0 || exit $?
;;
*)
echo "Usage: /etc/init.d/$daemon_NAME {start|stop|force-stop|restart|reload|force-reload|status}"
exit 1
;;
esac
exit 0
EOM
sudo chmod +x /etc/init.d/slideshow.sh
sudo chmod 0755 /etc/init.d/slideshow.sh
sudo chmod +x /home/pi/imgs/slideshow.sh
sudo chmod 0755 /home/pi/imgs/slideshow.sh
sudo systemctl daemon-reload
sudo update-rc.d slideshow.sh defaults

Related

script to check script is running and start it, if it’s stopped

I have a script with this name : Run.sh
I run this script with this command :
./run.sh
I don't like stop this script but this script Suddenly stops and need run again.
I need a script to check it , if my run.sh stopped , run it again.
this is run.sh codes:
#!/usr/bin/env bash
install() {
sudo apt-get update
sudo apt-get upgrade
}
if [ "$1" = "install" ]; then
install
else
if [ ! -f ./tg/tgcli ]; then
echo "tg not found"
echo "Run $0 install"
exit 1
fi
#sudo service redis-server restart
#./tg/tgcli -s ./bot/bot.lua -l 1 -E $#
./tg/tgcli -s ./bot/bot.lua $#
fi
And i want run this script at boot (with screen or tmux) if my server restart
i have Ubuntu 16.04 version
Thank you Ljm Dullaart
Can you help me about this ?
You should not need to run the complete bash script again. Changing
./tg/tgcli -s ./bot/bot.lua $#
to
while :; do
./tg/tgcli -s ./bot/bot.lua $#
done
will restart bot.lua everytime it exits.
You can check if your run.sh is running and re-run it if stopped with a single command:
$ if ! pgrep run.sh ;then /path/to/run.sh;fi
If script runs pgrep will return exit status 0 = success and will print the pid of run.sh
If script does not run pgrep will return exit status 1 and then script will be called.
You can also use pgrep inst.sh >/dev/null to "mute" pgrep in case script is running.

Run jar file as Daemon on Linux Ubuntu

I want to install a bot to my Teamspeak3 and run this bot as a daemon on startup. I wrote my own script and copied it to init.d and then added it with update-rc.d to defaults.
#!/bin/sh
#
# JTS3ServerBot Script
#
USER="ts"
NAME="jts3"
DIR="/home/ts/jts3/"
case $1 in
start)
echo "Starting ${NAME} ..."
if [ ! -f $DIR/pid ]; then
sudo -u $USER -c nohup java -jar $DIR/JTS3ServerMod.jar $DIR 2>> /dev/null >> /dev/null &
echo $! > $DIR/pid
echo "${NAME} started ..."
else
echo "${NAME} is already running ..."
fi
;;
stop)
if [ -f $DIR/pid ]; then
PID=$(cat $DIR/pid);
echo "Stopping ${NAME} ..."
kill $PID;
echo "${NAME} stopped ..."
rm $DIR/pid
else
echo "${NAME} is not running ..."
fi
;;
restart)
if [ -f $DIR/pid ]; then
PID=$(cat $DIR/pid);
echo "Stopping ${NAME} ...";
kill $PID;
echo "${NAME} stopped ...";
rm $DIR/pid
echo "Starting ${NAME} ..."
sudo -u $USER -c nohup java -jar $DIR/JTS3ServerMod.jar $DIR 2>> /dev/null >> /dev/null &
echo $! > $DIR/pid
echo "${NAME} started ..."
else
echo "${NAME} is not running ..."
fi
;;
esac
A pid file in generated, but if i try to kill the process with this pid i get an error that the process does not exist. If i use top there is no process with the pid listed.
root#vps-1023645-8462:~# service jts3 start
Starting jts3 ...
jts3 started ...
root#vps-1023645-8462:~# cat /home/ts/jts3/pid
10206
root#vps-1023645-8462:~# kill 10206
bash: kill: (10206) - No such process
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1762 ts 20 0 1881m 14m 3408 S 0 1.4 215:47.28 ts3server_linux
32356 ts 20 0 164m 1576 1336 S 0 0.2 0:09.85 tsdnsserver_lin
I have found another solution for my problem. I use upstart (works only with Ubuntu) to run my jar-File as a daemon. Upstart manages the PIDs. Just add myservice.conf to /etc/init (not /etc/inid.d) and the daemon will be started on boot and you can mangage it as a service. You do not have to make the file runnable or anything else
You can manage the service as normal for example
service myservice restart
service myservice status
...
My Config-File:
description "myservice"
author "your name"
start on runlevel [3]
stop on shutdown
expect fork
script
cd /home/username/
sudo -u username java -jar /home/username/myservice/myservice.jar >/home/username/myservice.log 2>&1
emit myservice_running
end script
This solution is really easy and works well on my Ubuntu 12.04 Server.
You have an error in this line:
sudo -u $USER -c nohup java -jar $DIR/JTS3ServerMod.jar $DIR 2>>/dev/null >>/dev/null&
You appear to be mixing the syntaxes of sudo and su. Before version 1.8, sudo had no -c option - you just give it the command to run after any other options. In 1.8 there is a -c option but it's not for specifying the command (it's for limiting resource usage to that of a given login class). sudo is printing an error message about this invalid syntax, but you're not seeing it because you're redirecting all the output to /dev/null.
Simply remove the -c to form a valid command:
sudo -u $USER nohup java -jar $DIR/JTS3ServerMod.jar $DIR 2>> /dev/null >> /dev/null &
Also, you can simplify the command a little by using the 2>&1 syntax to send stderr to the same handle as stdout, and there is no need for append mode when writing to /dev/null:
sudo -u $USER nohup java -jar $DIR/JTS3ServerMod.jar $DIR >/dev/null 2>&1 &

Escaping variables in bash file

I'm creating a init.d script within a bash file, which goes as follows:
# AUTOSTART
$APPDIR=somedir
$APPCONF=somedir
$APPVENV=somedir
$APPUSER=someuser
cat <<EOF >/etc/init.d/uwsgi
#!/bin/bash
daemon=$APPVENV/bin/uwsgi
args="--emperor $APPCONF/uwsgi/app.ini --daemonize /var/log/emperor.log --emperor-pidfile $APPDIR/emperor.pid --gid `id -g $APPUSER`"
pid=$APPDIR/emperor.pid
case "$1" in
start)
echo "Starting uwsgi"
start-stop-daemon -m -p $pid --start --exec $daemon $args
;;
stop)
echo "Stopping script uwsgi"
start-stop-daemon --signal INT -p $pid --stop $daemon $args
;;
reload)
echo "Reloading conf"
kill -HUP $(cat $pid)
;;
*)
echo "Usage: /etc/init.d/uwsgi {start|stop|reload}"
exit 1
;;
esac
exit 0
EOF
It is my understanding, from help, that $APPCONF, $APPVENV, $APPUSER and $APPDIR need to be escaped because I define them outside the file. So is it correct that I simply put a back slash in front of the variable like this:
daemon=\$APPVENV/bin/uwsgi
args="--emperor \$APPCONF/uwsgi/app.ini --daemonize /var/log/emperor.log --emperor-pidfile \$APPDIR/emperor.pid --gid `id -g \$APPUSER`"
pid=\$APPDIR/emperor.pid
It still doesn't seem to work though, the service doesn't start, so I think I might have done something else wrong. Can anyone confirm I am escaping properly please?
The dollar sign is only used for reading/using the value of a variable, not when setting its value. Thus, setting APPDIR to somedir would look like this:
APPDIR=somedir
Depending on how you call the script, you may also want to export the variable:
export APPDIR=somedir

Init.d script doesn't start or stop only prints help message

I've tried to write a custom upstart script for uwsgi emperor but it doesn't seem to start uwsgi and only says Usage: /etc/init.d/uwsgi {start|stop|reload} when I try to run it using "service uwsgi start".
Can anyone please tell me where I have gone wrong. The snippet below is from my deployment shell script:
cat <<EOF >/etc/init.d/uwsgi
#!/bin/bash
daemon=\$APPVENV/bin/uwsgi
args="--emperor \$APPCONF/uwsgi/app.ini --daemonize /var/log/emperor.log --emperor-pidfile \$APPDIR/emperor.pid --gid `id -g \$APPUSER`"
pid=\$APPDIR/emperor.pid
case "$1" in
start)
echo "Starting uwsgi"
start-stop-daemon -m -p \$pid --start --exec \$daemon \$args
;;
stop)
echo "Stopping script uwsgi"
start-stop-daemon --signal INT -p \$pid --stop \$daemon \$args
;;
reload)
echo "Reloading conf"
kill -HUP \$(cat \$pid)
;;
*)
echo "Usage: /etc/init.d/uwsgi {start|stop|reload}"
exit 1
;;
esac
exit 0
EOF
chmod u+x /etc/init.d/uwsgi
update-rc.d uwsgi defaults
service uwsgi start
Use the following. Pay close attention to which $ I escape and do not escape.
cat <<EOF >/etc/init.d/uwsgi
#!/bin/bash
daemon="$APPVENV/bin/uwsgi"
args=( --emperor "$APPCONF/uwsgi/app.ini"
--daemonize /var/log/emperor.log
--emperor-pidfile "$APPDIR/emperor.pid" --gid \$(id -g "$APPUSER")
)
pid="$APPDIR/emperor.pid"
case "\$1" in
start)
echo "Starting uwsgi"
start-stop-daemon -m -p \$pid --start --exec \$daemon \$args
;;
stop)
echo "Stopping script uwsgi"
start-stop-daemon --signal INT -p \$pid --stop \$daemon \$args
;;
reload)
echo "Reloading conf"
kill -HUP \$(< \$pid)
;;
*)
echo "Usage: /etc/init.d/uwsgi {start|stop|reload}"
exit 1
;;
esac
exit 0
EOF
chmod u+x /etc/init.d/uwsgi
update-rc.d uwsgi defaults
service uwsgi start
Variables like APPDIR, based on your previous questions, are used to configure what is actually written to disk, so you leave them unescaped so that they are expanded when /etc/init.d/uwsgi is written.
Variables like daemon, $1, and the command substitutions $(id -g "$APPUSER") are intended to be expanded when the init script runs, so you want the literal string $daemon to appear in the script, not the value of $daemon (which is probably undefined) when uwsgi is written.
You forgot to escape the $1 like you did in other places in your script:
case "\$1" in
As you are using cat to create the script, you need to escape all instances of $
Check if the service start really does send an argument to your script:
*)
echo "Argument taken was \"$1\"."
echo "Usage: /etc/init.d/uwsgi {start|stop|reload}"
exit 1
It could actually be different and you may consider making changes based from it.

Start Stop Daemon Init Debian - Unrecognized arguments

This is my debian init script so far to start uwsgi in emperor mode:
cat <<EOF >/etc/init.d/uwsgi
#!/bin/bash
daemon="$APPVENV/bin/uwsgi"
args=( --emperor "$APPCONF/uwsgi/app.ini"
--daemonize /var/log/emperor.log
--emperor-pidfile "$APPDIR/emperor.pid" --gid \$(id -g "$APPUSER")
)
pid="$APPDIR/emperor.pid"
case "\$1" in
start)
echo "Starting uwsgi"
start-stop-daemon -m -p \$pid --start --exec \$daemon \$args
;;
stop)
echo "Stopping script uwsgi"
start-stop-daemon --signal INT -p \$pid --stop \$daemon \$args
;;
reload)
echo "Reloading conf"
kill -HUP \$(< \$pid)
;;
*)
echo "Usage: /etc/init.d/uwsgi {start|stop|reload}"
exit 1
;;
esac
exit 0
EOF
chmod u+x /etc/init.d/uwsgi
update-rc.d uwsgi defaults
service uwsgi start
When I run this command I get this response:
> service uwsgi stop
Stopping script uwsgi
start-stop-daemon: unrecognized option '--emperor'
Try 'start-stop-daemon --help' for more information.
However, I know --emperor is a perfectly valid argument of uwsgi. So what's going on, am I putting the uwsgi arguments in the wrong place?
Any help would be appreciated.

Resources