hippy
Posts: 8578
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Jessie: How to run a script AFTER boot is done?

Sun Oct 04, 2015 7:31 pm

hippy wrote:Also, if anyone knows what [Unit]Before=, [Unit]After= and [Install]WantedBy= settings should be to have something executing when systemd completes that would save me some researching and experimentation.
Having thought about it, in terms of executing something at the end of boot-up; that no longer appears to be a valid concept with systemd. You simply need to have had a thing run before it is needed to have been run.

So just use whatever After= is required to have prerequisites set-up before it runs ( networking, syslog etc ), and WantedBy=multi-user.target. Unless I have misunderstood something.

AlKD
Posts: 1
Joined: Tue Oct 06, 2015 12:07 pm

Re: Jessie: How to run a script AFTER boot is done?

Tue Oct 06, 2015 1:31 pm

@hippie

Thank you for your contribution on systemd on Raspbian Jessie. :)

I could not do the usual automatic startup at boot after installing Jessie from Wheezy. The normal crontab -e @boot /home/user21/ovpns/pi_openvpn_server did not work anymore. I tried /etc/init.d and systemd, but to no avail.

All I need was a very simple entry point to launch a shell script at boot. I tried the codes from hippie, and viola, it did solved my pressing problem.

I am re-writing something very similar to hippie for the benefit to all Jessie Raspbian users.



STEP 1:

sudo nano /etc/systemd/system/pi_openvpn_server.service

[Unit]
Description=Raspberry PI OpenVPN Server
After=network.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/home/user21/ovpns/pi_openvpn_server
ExecReload=/home/user21/ovpns/pi_openvpn_server
WorkingDirectory=/home/user21/ovpns

[Install]
WantedBy=multi-user.target



STEP 2: (Both korn and bash shell work for openvpn_server_script)

sudo nano /home/user21/ovpns/pi_openvpn_server

#!/bin/sh -e
. /home/user21/ovpns/openvpn_server_script
exit 0



STEP 3:

It may be necessary to comment out the line, local 192.168.71.123, in openvpn_svr.conf, or the systemd could fail.

sudo nano /home/user21/ovpns/openvpn_svr.conf

;##local 192.168.71.123
dev tun
proto tcp
port 443
tun-ipv6
push tun-ipv6



STEP 4:

sudo systemctl enable pi_openvpn_server.service

sudo systemctl daemon-reload

sudo reboot



EXTRA:

Run the following two commands whenever editing and changes to the files were done.

sudo systemctl daemon-reload

sudo reboot



The above, work for me unexpected as I initially had the impression that systemd could be complicated to setup with the start, stop, reload at the bottom of the file, with the do_start, do_stop and do_reload functions in the middle and setting the system environment at the top. Also, it could be daunting to learn and to know how the systemd commands behave. As hippie has correctly pointed out, a simple stub to launch any korn or bash shell script is needed in systemd.


As for OpenVPN, Jessie is a major improvement over Wheezy. The OpenVPN 2.3.4 and OpenSSL 1.0.1k is required for the major security improvements. Until a few days ago, I had failed to install Fedora 22 onto Raspberry PI 2 many times. Now, I do not need to look into other Linux distributions for PI. Jessie is the trusted one with strong support from Raspbain and Debian. Thank you to the Raspberry Foundation team in porting Jessie onto the Raspberry PI.



AlKD
Malaysia

diederik
Posts: 394
Joined: Wed Mar 26, 2014 11:17 pm

Re: Jessie: How to run a script AFTER boot is done?

Tue Oct 06, 2015 9:07 pm

I haven't read all the posts, but 'systemd-analyze' can be of help; do 'systemd-analyze --help' to get a list of commands/options you can pass to it.
'systemd-analyze blame' is useful if you want to find out which component is most responsible for your boot time.
'systemd-analyze critical-path' will give you a hint as to what to put in the 'After' field of the unit file ;)

hippy
Posts: 8578
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Jessie: How to run a script AFTER boot is done?

Wed Oct 07, 2015 11:09 am

"critical-chain" rather than "critical-path" but thanks for the pointer to systemd-analyze.`

cmegens
Posts: 28
Joined: Fri May 24, 2013 12:23 pm
Location: Netherlands

Re: Jessie: How to run a script AFTER boot is done?

Fri Mar 11, 2016 2:22 pm

I still can't get my raspberry to perform a boot task that was simple under Wheezy; email me my ip address after boot. I'm fine with learning systemd, I'm also ok that things are a bit more complicated that editing rc.local, but this really is a pain....

I'll show you the steps I use:

First of all I created a shell script to email me my ip address using this script created by San Bergmans.

Code: Select all

#! /bin/bash

# mailmyip
#
# This script sends a mail every time the network comes up.
# The mail contains a time stamp and the obtained IP address.
#
# Use: mailmyip [interfacename]
#
# Author: San Bergmans
#         www.sbprojects.net
#

# Configuration variables
RPINAME="Raspberry Pi"
MAILTO="san.bergmans@gmail.com"

# Get interface name from parameters
if [ $# -eq 0 ]
then
        IFC="eth0"
else
        IFC="$1"
fi

ifconfig $IFC &> /dev/null
if [ $? -ne 0 ]
then
        exit 1
fi

# Get current private IP address for the selected interface
PRIVATE=$(ifconfig $IFC | grep "inet addr:" | awk '{ print $2 }')
IPV6=$(ifconfig $IFC | grep "Scope:Global" | awk '{ print $3 }')
PRIVATE=${PRIVATE:5}

# Exit if IP address is empty
if [ -z $PRIVATE ]
then
    exit 0
fi

# Wait about 2 minutes for the RTC to be set after boot (in steps of 10 seconds)
for I in {1..12}
do
    sleep 10
    if [ $(date +%Y) != "1970" ]
    then
        # Yes! The clock is set. Find out what our public IP address is and send the message.
                PUBLIC=$(curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//')
        MSG="$(date +%F\ %T)\n$RPINAME $IFC now has IP-Address $PRIVATE.\nIts public address is $PUBLIC.\n$IPV6"
        echo -e $MSG | mail -s "$RPINAME just received a new IP address" "$MAILTO"
        exit 0
    fi
done
After making that script executable with chmod +x I and setting up the mail command it emails me my ip address when I run it. So script works!

Then, I create a bootscript.service:

Code: Select all

[Unit]
Description=BOOTSCRIPT STARTER TO START SCRIPTS AT BOOT
After=network-online.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/home/pi/mailmyip
ExecReload=/home/pi/mailmyip
WorkingDirectory=/home/pi
StandardOutput=tty

[Install]
WantedBy=multi-user.target
After this I use:
sudo systemctl enable bootscript.service

sudo systemctl daemon-reload

sudo reboot
Now, if I am not mistaken, this should work, as it follows all the tutorials out there to use systemd to run a script at boot. I also added an echo to the mailmemyip script, in purple and caps, to see whether it would show up in the boot sequence and it does. What doesn't happen though is that I receive an email with my ip address. So script runs, but no email. There should be a connection and an ip address, so no clue what is wrong.
sudo systemctl -l bootscript.service

Gives me:
Unknown operation 'bootscript.service'.
Which is weird, right?

Anybody any idea what I'm doing wrong?

quinness
Posts: 17
Joined: Thu Mar 10, 2016 12:02 pm

Re: Jessie: How to run a script AFTER boot is done?

Fri Mar 11, 2016 10:47 pm

Sorry for the copy-and-paste of another file (thanks to whoever)
Does vnc be run by a user? in this case 'pi' [sudo -u pi]
my file, /etc/init.d/TightVNCServer

Code: Select all

sudo nano /etc/init.d/TightVNCServer

Code: Select all

!/bin/sh
#
# description: Starts and stops the TightVNCServer daemon
#
### BEGIN INIT INFO
# Provides: tightvncserver
# Required-Start: $network $syslog $remote_fs
# Required-Stop: $network
# Default-Start: 2 3 5
# Default-Stop: 0 1 6
# Short-Description: TightVNCServer daemon
# Description: Start or stop the TightVNCServer daemon
### END INIT INFO

VNC_BIN=/usr/bin/tightvncserver
test -x $VNC_BIN || { echo "$VNC_BIN not installed";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 5; fi; }

#CONFIG=/etc/my_vnc.conf
#test -f $CONFIG || { echo "no configuration found";
#        if [ "$1" = "stop" ]; then exit 0;
#        else exit 5; fi; }

# Source function library.
# . /etc/rc.status
# rc_reset

# Avoid using root's TMPDIR
#unset TMPDIR

#RETVAL=0

case "$1" in
  start)
        echo -n "Starting TightVNCServer daemon "
        sudo -u pi $VNC_BIN  :1 -geometry 1280x900 -depth 24 -dpi 96
       # rc_status -v
        ;;
  stop)
        echo -n "Shutting down TightVNCServer daemon "
        /sbin/killall $VNC_BIN
       # rc_status -v
        ;;
#  try-restart|condrestart)
        ## Do a restart only if the service was active before.
        ## Note: try-restart is now part of LSB (as of 1.9).
        ## RH has a similar command named condrestart.
#        if test "$1" = "condrestart"; then
#                echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
#        fi
#        $0 status
#        if test $? = 0; then
#                $0 restart
#        else
#                rc_reset        # Not running is not a failure.
#        fi
        # Remember status and be quiet
       # rc_status
#        ;;

  restart)
        $0 stop
        $0 start
       # rc_status
        ;;
#  force-reload)
#        $0 try-restart
       # rc_status
#        ;;
#  reload)
       # rc_failed 3
       # rc_status -v
#        ;;
  status)
        echo -n "Checking for TightVNCServer service "
#        /sbin/checkproc $VNC_BIN
       # rc_status -v
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload}"
        exit 1
esac
#rc_exit

Code: Select all

sudo chmod +x /etc/init.d/TightVNCServer
sudo systemctl enable TightVNCServer # added 'sudo '
sudo systemctl daemon-reload # added 'sudo '
Raspian Jessie 2016-02-26 _-=* If there are choices, why complain? *=-_
openSUSE LEAP 42.1 (3 pcs) _-=* Uppland, Sweden *=-_

quinness
Posts: 17
Joined: Thu Mar 10, 2016 12:02 pm

Re: Jessie: How to run a script AFTER boot is done?

Fri Mar 11, 2016 11:18 pm

Cleaned up script
edit your rc-script, in this case __that_software__

Code: Select all

sudo nano /etc/init.d/__that_software__
/etc/init.d/__that_software__

Code: Select all

#!/bin/sh
#
# description: Starts and stops the __that_software__ daemon
#
### BEGIN INIT INFO
# Provides: __that_software__
# Required-Start: $network $syslog $remote_fs
# Required-Stop: $network
# Default-Start: 2 3 5
# Default-Stop: 0 1 6
# Short-Description: __that_software__ daemon
# Description: Start or stop the __that_software__ daemon
### END INIT INFO

THAT_SOFTWARE_BIN=/usr/bin/__that_software__
test -x $THAT_SOFTWARE_BIN || { echo "$THAT_SOFTWARE_BIN not installed";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 5; fi; }

RETVAL=0

case "$1" in
  start)
        echo -n "Starting __that_software__ daemon "
        sudo -u pi $THAT_SOFTWARE_BIN  # Add your parameters after
        ;;
  stop)
        echo -n "Shutting down __that_software__ daemon "
        /sbin/killall $THAT_SOFTWARE_BIN
        ;;
  restart)
        $0 stop
        $0 start
       # rc_status
        ;;
  status)
        echo -n "Checking for __that_software__ service "
#        /sbin/checkproc $THAT_SOFTWARE_BIN
       # rc_status -v
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload}"
        exit 1
esac

Code: Select all

sudo chmod + x /etc/init.d/__that_software__
sudo systemctl enable __that_software__
sudo systemctl daemon-reload
Raspian Jessie 2016-02-26 _-=* If there are choices, why complain? *=-_
openSUSE LEAP 42.1 (3 pcs) _-=* Uppland, Sweden *=-_

Bosse_B
Posts: 1074
Joined: Thu Jan 30, 2014 9:53 am

Re: Jessie: How to run a script AFTER boot is done?

Sat Mar 12, 2016 6:45 am

Regarding getting TightVNC to run at boot:
I had the same problems on Jessie that it is hard to get something done on startup and I still have that for GPSD.
However after finding xanthor's thread here about starting TightVNC VNC started working for me!
You might find it useful.
Bo Berglund
Sweden

quinness
Posts: 17
Joined: Thu Mar 10, 2016 12:02 pm

Re: Jessie: How to run a script AFTER boot is done?

Sat Mar 12, 2016 8:41 am

Can you test this

Code: Select all

sudo nano /etc/init.d/gpsd

Code: Select all

#!/bin/sh
#
# description: Starts and stops the GPS daemon
#
### BEGIN INIT INFO
# Provides: gpsd
# Required-Start: $network $syslog $remote_fs
# Required-Stop: $network
# Default-Start: 2 3 5
# Default-Stop: 0 1 6
# Short-Description: GPS daemon
# Description: Start or stop the GPS daemon
### END INIT INFO

GPSD_BIN=/usr/sbin/gpsd
test -x $GPSD_BIN || { echo "$GPSD_BIN not installed";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 5; fi; }

RETVAL=0

case "$1" in
  start)
        echo -n "Starting GPS daemon "
        sudo -u pi $GPSD_BIN -N -b -N -D 3 /dev/ttyUSB0 -n -F /var/run/gpsd.sock
        ;; 
  stop)
        echo -n "Shutting down GPS daemon "
        /sbin/killall $GPSD_BIN
        ;;
  restart)
        $0 stop
        $0 start
       # rc_status
        ;;
  status)
        echo -n "Checking for GPSD service "
#        /sbin/checkproc $GPSD_BIN
       # rc_status -v
        echo $"Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload}"
        exit 1esac

Code: Select all

sudo chmod + x /etc/init.d/gpsd
sudo systemctl enable gpsd
sudo systemctl daemon-reload
Raspian Jessie 2016-02-26 _-=* If there are choices, why complain? *=-_
openSUSE LEAP 42.1 (3 pcs) _-=* Uppland, Sweden *=-_

Bosse_B
Posts: 1074
Joined: Thu Jan 30, 2014 9:53 am

Re: Jessie: How to run a script AFTER boot is done?

Sat Mar 12, 2016 10:55 am

Just to mention:
I do have a script that if executed manually on the command line it starts the gpsd daemon just fine.
And it runs on boot too, I have included a timestamp logging function in it to make sure that I can see it running.
So when systemd runs that script on boot it produces the logfile with the timestamp both before and after a sleep I have put into it, but even so the gpsd daemon does not start....
I have tested up to 30 seconds sleep before actually starting gpsd but no luck. :(
But as soon as I log in via SSH after boot I can execute the script from the console and gpsd starts just fine.
So it is not about the actual script but the way systemd runs it on start..

Code: Select all

#!/bin/sh -e
#Need to wait past boot so everything is done
date "+%Y-%m-%d %H:%M:%S" > /booted.txt
sleep 20
echo "Starting GPSD" >> /booted.txt
gpsd /dev/ttyUSB0 -G -n -F /var/run/gpsd.sock
date "+%Y-%m-%d %H:%M:%S" >> /booted.txt
exit 0
Bo Berglund
Sweden

quinness
Posts: 17
Joined: Thu Mar 10, 2016 12:02 pm

Re: Jessie: How to run a script AFTER boot is done?

Sat Mar 12, 2016 11:14 am

Just to clear
And it runs on boot too,
The script runs but doesn't start gpsd?
As I woozy questioned in the other thread, does gpsd starts if you start xgps (or other)? (If you run over ssh, add -X to the parameter list so you can start X application)
Have you tested the "new" init script for gpsd in the post above? It is a good start to use in systemd systems, you can use the normal systemctl options, start, stop, status, enable and disable.
It can be used for other commands, edit and change BIN variable (software) and adjust parameter passed to that software, setup for systemd.
Raspian Jessie 2016-02-26 _-=* If there are choices, why complain? *=-_
openSUSE LEAP 42.1 (3 pcs) _-=* Uppland, Sweden *=-_

quinness
Posts: 17
Joined: Thu Mar 10, 2016 12:02 pm

Re: Jessie: How to run a script AFTER boot is done?

Sat Mar 12, 2016 11:38 am

... and have you tested to add su -u pi to gpsd /dev/ttyUSB0 -G -n -F /var/run/gpsd.sock so it looks

Code: Select all

su -u pi gpsd /dev/ttyUSB0 -G -n -F /var/run/gpsd.sock
...?
Raspian Jessie 2016-02-26 _-=* If there are choices, why complain? *=-_
openSUSE LEAP 42.1 (3 pcs) _-=* Uppland, Sweden *=-_

User avatar
IgorGanapolsky
Posts: 33
Joined: Sun Apr 10, 2016 8:32 pm

Re: Jessie: How to run a script AFTER boot is done?

Mon Aug 08, 2016 5:41 am

Bosse_B wrote:I need to run a user level script to start VNC server directly after boot.
The old init.d scripting does not work anymore on Jessie, so I have to figure out a different way not involving a manual login via PuTTY to launch the server.
I do have a vnc.sh script in the pi home directory which if executed will start the VNC server.
It contains this:

Code: Select all

#!/bin/sh
vncserver :1 -geometry 1280x900 -depth 24 -dpi 96
It starts the server on port #5901 and after it has been run manually all is fine.
But I would rather have it done automatically upon boot....
Is there any way to detect that boot is done and launch it?
Have you tried putting @reboot entry into your crontab via `crontab -e`?

Return to “Raspberry Pi OS”