How to automatically execute shell script at startup boot on systemd Linux - LinuxConfig.org

The following example below will serve as a basic template to be later modified to suit your specific needs. In the example below we will check a disk space of a /home/ directory during a boot time and write a report to /root/ directory.


This is a companion discussion topic for the original entry at https://linuxconfig.org/how-to-automatically-execute-shell-script-at-startup-boot-on-systemd-linux

BrunoBronosky -> Guest

Linux works by having specific text in files in specific locations. There is no clicky graphic interface. The file system is the Registry™. Lot’s of projects have built GUI interfaces to run on Linux. But Linux is not GUI.

Corey Drew Bruce -> Guest

Yes there is, in Xubuntu for example you just go to session and startup to see your startup applications or add your own to enable or disable. Different desktop environments will have there own applications for this.

Gaurav Mittal

Hi,

I have exactly did the above steps. and when I execute the start of the service I see in status that the script gets executed. But when I check the process, nothing is there. The only difference is i am executing the script as a different user.

[dmsadmin@DMS-Admin system]$ cat DMSStartup.service
[Unit]
Description = Starting the DMS Weblogic Server

[Service]
User=dmsadmin
Group=dmsgroup
ExecStart = /opt/dms/domains/DMSDomain/bin/dmsAutoSrv.sh

[Install]
WantedBy = multi-user.target
[dmsadmin@DMS-Admin system]$

Mayur

Sir, am just a beginner in linux , can you please provide how to make systemctl service to start and also stop during reboot.
I have script in example/test which start and stop , /example/test/script start or stop , I want to execute this during reboot, please can u give all the details so just copy it from here, am stuck

Jorge

Thank you very much for this post, really usefull!

Jameson Pugh

Should probably include Type=OneShot

Gerard -> Jameson Pugh

I think oneshot has to be lower case (at least on Ubuntu 16)

Edward Crosby

For some reason I was not able to enable the service. I was testing within Fedora 24. I receive the following error:

# systemctl enable disk-space-check.service
Failed to enable unit: No such file or directory

UPDATE:
Never mind on this. I was able to complete the steps as root. It may be best in your article to mention that at the beginning.

reader

Please delete my previous comment and use this but without this line as I made an error.
After doing some research, I figured out what the problem was. I was
missing the “Type” parameter in the [Service] section. I now created two
services and I successfully enabled them. I no longer get the
error.

reader

I followed your instructions but upon running “systemctl enable disk-space-check.service” I got “Failed to execute operation: Unit file is masked” - this is in XUbuntu 16.04 Xenial. Any ideas?

Well this saved me a lot of trouble shooting on why my Opensuse Leap 15.2 box just didn’t want to bind pci vfio to my GPU.

I solved it by just adding this to the “script”
(my gpu is listed as 03:00.x, but it failed to bind to 00.1 , 00.2, 00.3 (audio, usb, usb-c)
echo “0000:03:00.1” > /sys/bus/pci/devices/0000:03:00.1/driver/unbind
… an for the rest of the “device”…
echo “waiting”
sleep 5
modprobe -a vfio-pc

I’m just writing this if somebody ever googles a keyword that matches this issue… and for my own future reference because I’ve had this problem before.