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
-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:
mkdir projects
cd projects
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:!/bin/bash
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/

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



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

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

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 for 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/'

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/'

# 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:
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
-c.b- edit
nano /home/pi/Documents/!/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/
and test with
-c.d- make a desktop file to execute it from desktop ...
nano /home/pi/Documents/mypresentation.desktop[Desktop Entry]
Comment=show on boot / login pi

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:
touch /run/shm/kilroy_was_here

chmod +x
test it and find in user ram disk that file
ls /run/shm/
way 1:
nano .config/autostart/myautostart.desktop[Desktop Entry]
Comment=myautostart of

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"


exit 0

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

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

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/ for Thu Mar 29 01:17:58 UTC 2018 ----------
started screen session MYSCREEN use: screen -r

auto started by add line to