Linux – How to create a custom service that will autostart on boot on Archlinux

arch linuxautostartlinuxservices

I'would like to run a simple command upon startup on Archlinux (systemd):

nohup fatrat -n &

I've got this working on Debian:

#! /bin/sh
# /etc/init.d/fatratWS

### BEGIN INIT INFO
# Provides: fatratWS
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: fatratWS init script.
# Description: Starts and stops fatrat Web Server services.
### END INIT INFO

#VAR
FATRAT_PID=$(ps aux | awk '/fatrat --nogui/ && !/awk/ && !/nohup/ {print $2}')

# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting script fatratWS"
if [ -z "$FATRAT_PID" ]; then
nohup fatrat --nogui &
echo "Started"
else
echo "fatratWS already started"
fi
;;
stop)
echo "Stopping script fatratWS"
if [ ! -z "$FATRAT_PID" ]; then
kill $FATRAT_PID
fi
echo "OK"
;;
status)
if [ ! -z "$FATRAT_PID" ]; then
echo "The fatratWS is running with PID = "$FATRAT_PID
else
echo "No process found for fatratWS"
fi
;;
*)
echo "Usage: /etc/init.d/fatratWS {start|stop|status}"
exit 1
;;
esac

exit 0

How can I achieve the same on Arch?

I've tried:

[Unit]
Description=Fatrat NoGui Web Access Service

[Service]
ExecStart=/usr/bin/nohup /usr/bin/fatrat -n &
Type=forking

[Install]
WantedBy=multi-user.target

But it fails to start when starting manually (timeout)

Best Answer

Try this:

[Unit]
Description=Fatrat NoGui Web Access Service
Requires=network.target
After=network.target

[Service]
ExecStart=/usr/bin/fatrat -n
Type=forking

[Install]
WantedBy=multi-user.target
  • I assumed, that a "Web Access Service" needs network, so I added network.target as a requirement.

  • Using nohup is unnecessary because this functionality is provided by systemd itself, same for the '&'.

  • Because we don't use nohup anymore, the type would change to simple, however, the web interface available on the git release won't work unless we make it forking.

  • For more information on systemd service files see the "systemd.service" man page and https://wiki.archlinux.org/index.php/Systemd#Writing_custom_.service_files

  • You might consider to add Restart=always to the [Service] section to get it restarted automatically if it crashes.

  • Put the service file at /etc/systemd/system/fatrat.service and enable it for automatic startup via systemctl enable fatrat.service