Running a script on reboot using systemd
(instead of cron
)
(Optional) Study the example service definitions in /usr/lib/systemd/system
.
Create a new service file in /etc/systemd/system
with a ".service" extension. Here's an example that will run a bash script at boot time:
[Unit]
Description=A Simple Script
[Install]
WantedBy=multi-user.target
[Service]
ExecStart=/bin/bash /path/to/script.sh
Type=simple
User=josh
Group=josh
WorkingDirectory=/home/josh
Restart=on-failure
sudo systemctl daemon-reload
sudo systemctl start <whatever>.service
sudo systemctl enable <whatever>.service
NOTE: According to the docs: "...shell command lines are not directly supported. If shell command lines are to be used, they need to be passed explicitly to a shell implementation of some kind. Example:"
ExecStart=sh -c 'dmesg | tac'"