Articles Hierarchy

Articles Home » RPI » RPI FAQ autostart

RPI FAQ autostart

one of the frequent questions at forum is: autostart something

now there is first to make clear when to start something
summary:
-a- at system boot you need a "service" to start ( or run continuously )
-b- after a user login ( also for the putty logins )
-c- show something on desktop on start


-a- at system boot
about that i already write some things, also making some nice ZENITY tools around that,
but there is a change in the OS RASPBIAN
using systemd now the startfile syntax changed.
to learn about that i follow this blog.
as i changed in the progress i offer the files packed here:
cd
mkdir projects
cd projects
wget http://kll.engineering-news.org/kllfusion01/downloads/service_autostart.tar.xz
tar -xvf service_autostart.tar.xz
cd service_autostart
ls -la
cat disk-space-check.txt
sudo rm disk-space-check.txt

now lets see what i tested ( according above mentioned blog ) there:

disk-space-check.sh#!/bin/bash
myPATH='/home/pi/projects/service_autostart/'
myFILE='disk-space-check.txt'
date > $myPATH$myFILE
du -sh /home/ >> $myPATH$myFILE
df /home/ --output=avail -h >> $myPATH$myFILE

this script i want to be executed after boot,
it creates a file disk-space-check.txt
what shows used disk space by /home/... means all users
and also how much space is still free on our system SD, looks like this:
Tue 14 Feb 13:20:50 ICT 2017
1.4G /home/
Avail
23G

when we want to execute this at boot / system start we need a start / service file
disk-space-check.service[Unit]
Description=user pi check disk space on SD /home/... (for all users)
After=network.target

[Service]
User=pi
ExecStart=/home/pi/projects/service_autostart/disk-space-check.sh

[Install]
WantedBy=multi-user.target

this waits until network is up, execute the script from its home dir ( what itself stores the result in its home dir), but as its done by ROOT at boot, you will find that you need to delete it using sudo.

this service file needs to be copied and started with
./disk-space-check.service.install.sh
disk-space-check.service.install.sh#!/bin/bash
myPATH='/home/pi/projects/service_autostart/'
myFILE='disk-space-check.service'


cd $myPATH
sudo cp $myFILE /etc/systemd/system/$myFILE
sudo chmod 664 /etc/systemd/system/$myFILE
sudo systemctl daemon-reload
sudo systemctl enable $myFILE

now check what happens after sudo reboot

check on service still run:
systemctl list-unit-files disk-space-check.service
check on result:
cat disk-space-check.txt

to remove this service pls use
./disk-space-check.service.remove.sh
disk-space-check.service.remove.sh#!/bin/bash
myPATH='/home/pi/projects/service_autostart/'
myFILE='disk-space-check.service'


cd $myPATH
sudo rm /etc/systemd/system/$myFILE
sudo systemctl disable $myFILE
sudo systemctl daemon-reload



-b- after a user login
one of the useful things when you work with terminal, ( from desktop or remote putty )
is to have some custom settings, done every time you open a terminal.
( means not use that to start an service as at second login , terminal.. you get the warnings that the service runs already)
if you need some thing executed or just remembered use
nano .bashrc
or in case of aliases better use
nano .bash_aliases what is called from .bashrc
so if you are a terminal user that's the thing for you.
.bash_aliases#!/bin/bash
#.bash_aliases for user pi

USER='pi'

alias di='ls -la'
# ...
alias work='cd /home/'$USER'/projects/my_new_project'
alias workedit='nano /home/'$USER'/.bash_aliases'

# disk
alias dh='df -T -h && lsblk'

# for use with MC only
alias mc='. /usr/lib/mc/mc-wrapper.sh'

alias update='sudo apt-get update && sudo apt-get -y upgrade'
alias vnc='vncserver :1 -geometry 1366x768'
alias show_service='service --status-all'
alias drop='/home/pi/Dropbox-Uploader/dropbox_uploader.sh'

# because even that are my most frequent CLI commands still i need to be remembered
echo 'use: work, workedit, di, dh, mc, mcedit, update, vnc, show_service, drop'

# check OS and cpu on board
echo -n 'DEBIAN ' && lsb_release -sc
cat /proc/version | grep -o "Linux version.........."
echo -n 'CPU: ' && lscpu | grep -o "armv.."
ifconfig | grep -o "inet addr:192.168......"
#echo -n 'firewall: ufw '
#sudo ufw status | grep 'Status'
echo -n 'cpu-' && /opt/vc/bin/vcgencmd measure_temp
echo -n 'date: ' && date
#

and it looks like:
$ . .bash_aliases
use: work, workedit, di, dh, mc, mcedit, update, vnc, show_service, drop
DEBIAN jessie
Linux version 4.4.38-v7
CPU: armv7l
inet addr:192.168.1.203
cpu-temp=48.3'C
date: Tue 14 Feb 20:12:51 ICT 2017



-c- show something on desktop on start
there was a easy question at forum,
how to auto start a presentation
what showed me that there never anything is easy.
also because i wanted to show the OP how to do that completely from desktop / filemanager / leafpad editor...
assuming he is not the terminal type.
that is a job for a video and not for a tutorial text...
here short form:
-c.a- there is a
/home/pi/Documents/mypresentation.odp
-c.b- edit
nano /home/pi/Documents/mypresentation.sh#!/bin/bash
env sleep 30s
/usr/bin/libreoffice --show /home/pi/Documents/mypresentation.odp

now that was the main problem i had there, that impress start must be delayed or it opens and locks the presentation, until you manually recover the file.
-c.c- make it executable
chmod +x /home/pi/Documents/mypresentation.sh
and test with
./mypresentation.sh
-c.d- make a desktop file to execute it from desktop ...
nano /home/pi/Documents/mypresentation.desktop[Desktop Entry]
Type=Application
Name=mypresentation
Exec=/home/pi/Documents/mypresentation.sh
Icon=libreoffice-impress
Comment=show on boot / login pi
Terminal=true
X-KeepTerminal=false

and test it via desktop filemanager go Documents double click on desktop icon.
-c.e- for autostart need to copy the desktop file
cp /home/pi/Documents/mypresentation.desktop /home/pi/.config/autostart/mypresentation.desktop
sudo reboot


example using /etc/xdg/autostart/


116 t 104 h 105 i 115 s 32 space 119 w 97 a 115 s 32 space 102 f 117 u 110 n



here again some short tests for autostart:
THE JOB:
cd
nano myautostart.sh#!/bin/bash
touch /run/shm/kilroy_was_here

chmod +x myautostart.sh
test it and find in user ram disk that file
ls /run/shm/
THE AUTOSTART:
way 1:
nano .config/autostart/myautostart.desktop[Desktop Entry]
Type=Application
Name=myautostart
Exec=/home/pi/myautostart.sh
Icon==gksu-root-terminal.png
Comment=myautostart of myautostart.sh

way 2:
sudo nano /etc/rc.local#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi

/home/pi/myautostart.sh

exit 0



update 23.10.2017
from a post at 4um i learned using systemd userlevel...



other example of USER autostart :
myautostart.sh
#!/bin/bash
# /home/pi/myautostart.sh
# called by
# nano /home/pi/.config/lxsession/LXDE-pi/autostart
# touch /run/shm/KILROY_WAS_HERE

LOGFILE=/home/pi/myautostart.log
LOGDATE=$(date -u)
touch $LOGFILE
echo "---------- Log of $0 for $LOGDATE ----------" >> $LOGFILE

# test job for myautostart

# need to install
# sudo apt install -y screen
# start a screen session with a detached window running app: htop
screen -d -m -S MYSCREEN htop &
echo "started screen session MYSCREEN use: screen -r" >> $LOGFILE

makes file
myautostart.log---------- Log of /home/pi/myautostart.sh for Thu Mar 29 01:17:58 UTC 2018 ----------
started screen session MYSCREEN use: screen -r

auto started by add line to
/home/pi/.config/lxsession/LXDE-pi/autostart@/home/pi/myautostart.sh