Run a script on Ubuntu 18 at startup
I have been trying for 2 days to send messages from the Ubuntu machine to slack directly without using any third-party tools. This is somehow practical as you don’t have to depend on any other tools to get notifications. But the blocker here was after several trial ubuntu was not running the scripts as intended.
There seem many ways to run the script on startup like using init.d and initializing run level and activating the service but in all processes, I got stuck at some point during the process. So I would like to go through the solution here rather than what I went through.
First of all, go to the folder /etc/systemd/system
There you will see many other files, ignore those for a moment and create a new file with my-service.service with the content from below, just copy paste and edit the ones that are not commented.
[Unit]Description=My custom startup script# After=network.target# After=systemd-user-sessions.service# After=network-online.target[Service]# User=spark# Type=simple# PIDFile=/run/my-service.pidExecStart=/home/transang/startup.sh start# ExecReload=/home/transang/startup.sh reload# ExecStop=/home/transang/startup.sh stop# TimeoutSec=30# Restart=on-failure# RestartSec=30# StartLimitInterval=350# StartLimitBurst=10[Install]WantedBy=multi-user.target
After editing files with proper name make sure that files exist in the location you have mentioned in the file above.
Then, execute the command
systemctl enable my-service.service
This will enable the service you created earlier
Now, execute
systemctl start my-service.service
You can also stop the service at some point if you like to stop it later.
Now goto /home/transang/startup.sh.
Edit the files with whatever you want to run at startup. Check by restarting the machine and see if that executes and troubleshoot further.