RUN LINUX SCRIPT AUTOMATICALLY AFTER THE SERVER REBOOTS / RESTARTS OR BOOTS

Mhualika

New Member
Joined
Aug 23, 2024
Messages
2
Reaction score
1
Credits
17
Hi All,
Can someone help me to know where shall i deploy a script so that it gets executed automatically everytime the server reboots or boots i.e. is up and running after the reboot, basically it should run once the server is rebooted and is about to start .
 


On KDE it's under Settings -> Startup & Shutdown -> Autostart
At this location is "Add" button to add your script.
 
There are a number of ways to have a script run on boot or startup, noting that it's a server.

If by chance the server is running a desktop or window manager, there may be a configuration in that software to add an executable script. For example, to pick one window manager such as icewm, in order to start some programs when icewm starts up, one can use a file ~/.icewm/startup within which there's a reference to the relevant script. The large DEs, gnome and KDE both have such configuration availabilities.

Just saw @CaffeineAddict provide some details in post #2 for KDE!

Another means, in a systemd system, independent of GUI apps, is to write a systemd script. For example, a format like the following in a systemd service file named <some-evocative-name>.service, with the following contents all properly specified to give effect to the script:
Code:
[Unit]
Description=<a_description_of_the_the_script's_function>

[Service]
Type=oneshot
ExecStart=/bin/sh -c <path_to_the_script>
 
[Install]
WantedBy=multi-user.target
When the file is written it can be placed in /etc/systemd/system, or some other relevant directory, enabled and it will run.

Another means, in a systemd system, is to use the /etc/rc.local file. The file needs to be created, given executable permissions, and have a reference to the script for its contents. Systemd will then run it at the end of the boot without any more ado.

If the machine is running a sysVinit system, then one needs to write a configuration file written into the appropriate runlevel in /etc, for example, in /etc/rc.3 for runlevel 3, or however the sysVinit is organised. The configuration of such a file has a specific format which needs to be adhered to which is quite a bit more involved than simply referring to the script as in the /etc/rc.local described above. Since most distros still include such files in /etc/init.d or thereabouts, one can inspect them to see how they are written. A sysVinit system may also honour the /etc/rc.local method, which can be simpler.

There are possible issues with starting scripts at boot which depend on what the scripts are doing. If, for example, a script needs to use a network, then that network has to be running by the time the script is called after boot. There are configuration options in the systemd unit files to get these matters happen in an orderly way. With the /etc/rc.local file however, such an issue would need care since a script that needs the network may fail if the network takes longer to get up than the time it takes the boot to run the script.
 
Last edited:
There are a number of ways to have a script run on boot or startup, noting that it's a server.

If by chance the server is running a desktop or window manager, there may be a configuration in that software to add an executable script. For example, to pick one window manager such as icewm, in order to start some programs when icewm starts up, one can use a file ~/.icewm/startup within which there's a reference to the relevant script. The large DEs, gnome and KDE both have such configuration availabilities.

Just saw @CaffeineAddict provide some details in post #2 for KDE!

Another means, in a systemd system, independent of GUI apps, is to write a systemd script. For example, a format like the following in a systemd service file named <some-evocative-name>.service, with the following contents all properly specified to give effect to the script:
Code:
[Unit]
Description=<a_description_of_the_the_script's_function>

[Service]
Type=oneshot
ExecStart=/bin/sh -c <path_to_the_script>
 
[Install]
WantedBy=multi-user.target
When the file is written it can be placed in /etc/systemd/system, or some other relevant directory, enabled and it will run.

Another means, in a systemd system, is to use the /etc/rc.local file. The file needs to be created, given executable permissions, and have a reference to the script for its contents. Systemd will then run it at the end of the boot without any more ado.

If the machine is running a sysVinit system, then one needs to write a configuration file written into the appropriate runlevel in /etc, for example, in /etc/rc.3 for runlevel 3, or however the sysVinit is organised. The configuration of such a file has a specific format which needs to be adhered to which is quite a bit more involved than simply referring to the script as in the /etc/rc.local described above. Since most distros still include such files in /etc/init.d or thereabouts, one can inspect them to see how they are written. A sysVinit system may also honour the /etc/rc.local method, which can be simpler.

There are possible issues with starting scripts at boot which depend on what the scripts are doing. If, for example, a script needs to use a network, then that network has to be running by the time the script is called after boot. There are configuration options in the systemd unit files to get these matters happen in an orderly way. With the /etc/rc.local file however, such an issue would need care since a script that needs the network may fail if the network takes longer to get up than the time it takes the boot to run the script.
Thank you so much
 
you can also use cron for this. exec
Code:
crontab -e
then enter
Code:
@reboot /path/to/script >> /dev/null 2>&1
If you want to wait f.e. 5 seconds, do this:
Code:
@reboot sleep 5s && /path/to/script >> /dev/null 2>&1
 


Latest posts

Top