Starting Wireless On Boot With systemd

I didn't want to use a GUI to manage my wireless network. Majority of the time I will be at known locations, and when needing to join a new network doing it via command line is not difficult.

I have known networks configured, with dhcpcd and wpa_supplicant started during bootup by systemd. I also chose to log, and rotate, wpa_supplicant to help debug any network issues I might encounter. The following was done in Arch.

A few files must exist, the first is a .service file under /usr/lib/systemd/system/. The Type=forking is required since we are starting a daemon, the PIDFile is optional. If you include it, make sure you actually create a pid file when starting the process.

#cat /usr/lib/systemd/system/wireless.service  
[Unit]  
Description=Wireless

[Service]  
Type=forking  
PIDFILE=/var/run/wireless.pid  
ExecStart=/usr/lib/systemd/scripts/wirelessStart  
ExecReload=/usr/lib/systemd/scripts/wirelessRestart  
ExecStop=/usr/lib/systemd/scripts/wirelessStop

[Install]  
WantedBy=multi-user.target

I chose to make individual Start, Restart and Stop scripts as seen above. You may do this differently. Here are mine:

#Start Script  
#cat /usr/lib/systemd/scripts/wirelessStart  
#!/bin/bash  
ps aux | grep wpa_supplicant | grep -v grep &>/dev/null || {  
wpa_supplicant -f /var/log/wpa_supplicant/wpa_supplicant.log -B -i
wlp8s0 -P/var/run/wireless.pid -c /etc/wpa_supplicant.conf -d  
}

#Stop Script - I haven't had an issue with pkill failing to get the job done.  
#cat /usr/lib/systemd/scripts/wirelessStop  
#!/bin/bash  
pkill wpa_supplicant

#Restart Script  
#cat /usr/lib/systemd/scripts/wirelessRestart  
#!/bin/bash  
/usr/lib/systemd/scripts/wirelessStop  
sleep 3  
/usr/lib/systemd/scripts/wirelessStart

logrotate:

#cat /etc/logrotate.d/wpa_supplicant  
/var/log/wpa_supplicant/*log {  
copytruncate  
compress  
notifempty  
missingok  
}

Enabling the new service, and checking status:

#enable the service  
[jpyth@arch-jpyth ~]$ sudo systemctl enable wireless  
ln -s '/usr/lib/systemd/system/wireless.service'
'/etc/systemd/system/multi-user.target.wants/wireless.service'  
#start the service  
[jpyth@arch-jpyth ~]$ sudo systemctl start wireless  
#status of service  
[jpyth@arch-jpyth ~]$ sudo systemctl status wireless  
wireless.service - Wireless  
Loaded: loaded (/usr/lib/systemd/system/wireless.service; enabled)  
Active: active (running) since Wed 2013-03-27 07:29:19 PDT; 3s ago  
Process: 1574 ExecStart=/usr/lib/systemd/scripts/wirelessStart
(code=exited, status=0/SUCCESS)  
Main PID: 1579 (wpa_supplicant)  
CGroup: name=systemd:/system/wireless.service  
└─1579 wpa_supplicant -f /var/log/wpa_supplicant/wpa_supplicant.log
-B -i wlp8s0 -P/var/run/wireless.pid -c /etc/wpa_supplicant.conf -d

Mar 27 07:29:18 arch-jpyth systemd[1]: Starting Wireless...  
Mar 27 07:29:19 arch-jpyth systemd[1]: Started Wireless.  
[jpyth@arch-jpyth ~]$ psg wpa_supplican  
root 1579 0.0 0.0 32328 1344 ? Ss 07:29 0:00 wpa_supplicant -f
/var/log/wpa_supplicant/wpa_supplicant.log -B -i wlp8s0
-P/var/run/wireless.pid -c /etc/wpa_supplicant.conf -d  
#log file  
[jpyth@arch-jpyth ~]$ tail
/var/log/wpa_supplicant/wpa_supplicant.log  
EAP: EAP entering state DISABLED  
EAPOL: SUPP_PAE entering state AUTHENTICATED  
EAPOL: Supplicant port status: Authorized  
EAPOL: SUPP_BE entering state IDLE  
EAPOL authentication completed successfully  
RTM_NEWLINK: operstate=1 ifi_flags=0x11043
([UP][RUNNING][LOWER_UP])  
RTM_NEWLINK, IFLA_IFNAME: Interface 'wlp8s0' added  
WEXT: if_removed already cleared - ignore event  
EAPOL: startWhen --> 0  
EAPOL: disable timer tick  
[jpyth@arch-jpyth ~]$

*note if you need dhcpcd enable/verify:

#enable the service  
[jpyth@arch-jpyth ~]$ sudo systemctl enable dhcpcd  
ln -s '/usr/lib/systemd/system/dhcpcd.service'
'/etc/systemd/system/multi-user.target.wants/dhcpcd.service'  
#start the service  
[jpyth@arch-jpyth ~]$ sudo systemctl start dhcpcd  
#status of the service  
[jpyth@arch-jpyth ~]$ sudo systemctl status dhcpcd  
dhcpcd.service - dhcpcd on all interfaces  
Loaded: loaded (/usr/lib/systemd/system/dhcpcd.service; enabled)  
Active: active (running) since Wed 2013-03-27 07:32:06 PDT; 4s ago  
Process: 1697 ExecStart=/sbin/dhcpcd -q -b (code=exited,
status=0/SUCCESS)  
Main PID: 1698 (dhcpcd)  
CGroup: name=systemd:/system/dhcpcd.service  
└─1698 /sbin/dhcpcd -q -b

Mar 27 07:32:06 arch-jpyth systemd[1]: Started dhcpcd on all
interfaces.  
Mar 27 07:32:06 arch-jpyth dhcpcd[1698]: wlp8s0: sending IPv6 Router
Solicitation  
Mar 27 07:32:06 arch-jpyth dhcpcd[1698]: wlp8s0: rebinding lease of
192.168.1.42  
Mar 27 07:32:06 arch-jpyth dhcpcd[1698]: enp7s0: waiting for carrier  
Mar 27 07:32:06 arch-jpyth dhcpcd[1698]: wlp8s0: acknowledged
192.168.1.42 from 192.168.1.1  
Mar 27 07:32:06 arch-jpyth dhcpcd[1698]: wlp8s0: checking for
192.168.1.42  
Mar 27 07:32:10 arch-jpyth dhcpcd[1698]: wlp8s0: sending IPv6 Router
Solicitation  
[jpyth@arch-jpyth ~]$
more ...

'psg'

Introduced to this years ago by a colleague, but it's still my favorite little alias.

#Add to ~/.bashrc  
alias psg='ps aux | grep -v grep | grep -i'  
#Source the updated bashrc(or relog)  
source ~/.bashrc  
#I prefer to not be case sensitive, example:  
[jpyth@arch-jpyth ~]$ psg fire  
jpyth 16131 12.7 3.7 1463388 607748 ? Sl Mar25 367:33
/usr/lib/firefox/firefox  
[jpyth@arch-jpyth ~]$ psg FiRe  
jpyth 16131 12.7 3.7 1463388 607748 ? Sl Mar25 367:33
/usr/lib/firefox/firefox  
[jpyth@arch-jpyth ~]$
more ...

Configuring irssi

autoconnect:

#server section of ~/.irssi/config  
servers = (  
{  
address = "$hostname";  
chatnet = "IRCnet";  
port = "6667";  
autoconnect = "true";  
password = "$user:$pass";  
}  
);

irssi logging:

#Create directory for logs  
#Large storage drive == /storage/  
sudo ln -s /storage/irssi /var/log/  
#You'll probably want to own these files or atleast rw access.  
chown -R $owner:$group /storage/irrsi  
:::bash  
#in irssi run:  
/SET autolog ON  
/SET autolog_path /var/log/irssi/$tag/$0.log  
/save

logrotate:

#Contents of /etc/logrotate.d/irssi  
/var/log/irssi/*/*log {  
copytruncate  
compress  
notifempty  
missingok  
}

irssi-libnotify-git from Arch User Repository (AUR):

#For xfce4  
sudo pacman -S xfce4-notifyd  
#Get package from AUR  
wget
https://aur.archlinux.org/packages/ir/irssi-libnotify-git/irssi-libnotify-git.tar.gz  
Extract && cd  
tar -xvf irssi-libnotify-git.tar.gz  
cd irssi-libnotify-git  
#create package - Install missing deps  
makepkg || {  
#missing deps  
sudo pacman -S python2-gobject perl-html-parser  
}  
makepkg  
#install final package  
sudo pacman -U irssi-libnotify-git*.pkg.tar.xz  
#create directories if missing && symlink installed script for irssi
to use.  
mkdir -p ~/.irssi/scripts/autorun && ln -s
/usr/share/irssi/scripts/notify.pl ~/.irssi/scripts/autorun/  
#dep for notify-listener.py  
sudo pacman -S python2-dbus  
#start listener -- I do this when launching irc. You could start it
with systemd/init too.  
nohup notify-listener.py &

Simple script to grab current or start a new irssi:

#!/bin/bash

screenN="irssi"  
notify=\`which notify-listener.py\`  
irc=\`which irssi\`

ps aux | grep "$notify" | grep -v grep &>/dev/null || {  
nohup $notify &  
}  
ps aux | grep -i "$irc" | grep -v grep &>/dev/null || {  
screen -S $screenN $irc  
} && {  
screen -list | grep "$screenN" | wc -l | grep "\^1$" &>/dev/null &&
{  
screen -D $screenN  
screen -R $screenN  
} || {  
echo "You has too many/few irssis, check screen"  
}  
}

Enjoy

more ...


Welcome

A self taught, constantly learning geek living the dream in Silicon Valley.

The majority of configuration content on this site will come from either Arch or CentOS, though concepts should apply to nearly any Linux system.

more ...