To make a binary file run on boot, you can create a startup script that will run the binary file when the operating system starts. The exact process for creating a startup script depends on the operating system you’re using. Here’s an example for a Linux operating system using the systemd init system:
Create a new script file in the /etc/systemd/system directory. For example, you could name it mybinary.service.
Add the following content to the file, replacing /path/to/mybinary with the actual path to your binary file:
Demo file
[Unit] Description=My Binary [Service] ExecStart=/path/to/mybinary Restart=always User=root [Install] WantedBy=multi-user.target Alias=myService.service
Note: If you want to run a shell script file, you can use this code below
ExecStart=/bin/bash /path/to/myscript.sh
Or
ExecStart=/bin/bash /path/to/myscript.sh &
Reload the systemd manager configuration:
sudo systemctl daemon-reload
Enable the service to start at boot and then start it:
#Enable sudo systemctl enable mybinary.service #Start the service: sudo systemctl start mybinary.service
After these steps, the binary file will run every time the operating system starts. You can use systemctl commands to manage the service, such as checking its status (systemctl status mybinary.service) or stopping it (systemctl stop mybinary.service).
Note: The process for creating a startup script may vary depending on the operating system and init system being used. If you’re using a different operating system or init system, you may need to follow different steps.
More guide on .service file
Or, if you want to run the script with nohup to run background and prevent it from being terminated , you might think of something like
ExecStart=nohup /path/to/myscript.sh &
or
ExecStart=nohup /bin/bash /path/to/myscript.sh &
it will both raise an error : Exec format error
The nohup command is used to run a command in the background and prevent it from being terminated when the terminal session ends. It is not a valid executable format for the ExecStart directive in a service file, which expects an executable binary or a script in a valid shell script format.
To resolve the issue, you can modify the ExecStart directive to run the script directly, without using nohup. For example, if your script is located at /path/to/script.sh, the ExecStart directive could be as follows: (with an & last)
ExecStart=/bin/bash /path/to/script.sh &
Another way to Make a binary file or script file run on startup on ubuntu
Use crontab
crontab -e
Add the following line at the end of the file, replacing /path/to/script.sh with the actual path to your script:
@reboot /bin/bash /path/to/script.sh &
REF