Saturday, September 15, 2018

post boot startup scripts

Going into "X", there are ways to run startups for various GUI applications. But what about at boot time? GRUB/LILO's job is only to initialize the kernel and the system: booting apps are not the way to, say, run an app that connects to wifi or runs a cron job. How to do? My ugly solution is a text rc.local file, added with a second step of creating and attaching a systemd service. Prior to systemd, in less complicated Linux years, we could create text app init files inside /etc/init.d/.

/etc/rc.local

There are more elegant ways (eg. as argued here) to connect to the web at startup, but let's use web connection as an easy rc.local example app, then pick your own application(s) to put into rc.local.

# nano /etc/rc.local
#!/bin/bash
echo "router connect"
wpa_supplicant -iwlan0 -Dwext -B -c /etc/wpasupplicant/wpa_supplicant.conf
exit 0

# chmod 755 /etc/rc.local
... or some like to just "+x" it.

/etc/systemd/system/rc-local.service


Next, create a systemd service file to call rc.local. As well noted here, the file must include a "wanted by" line.

# nano /etc/systemd/system/rc-local.service
[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99

[Install]
WantedBy=multi-user.target
... and then enable the service in systemd...
# systemctl enable rc-local.service

The next time the system boots, it subsequently runs the rc.local file and connects to the router.

No comments: