To add your own binary or script to startup with systemd, you need to create a new service file and enable it. Here's a step-by-step guide for a typical use case:
Write a Service Unit File
Create a new service unit file in /etc/systemd/system/, for example, . Use a text editor like or (you’ll need root privileges to create files in this directory):
User
Could bashrc also be used? The binary is running on linux machines
Assistant
Yes, .bashrc (or other shell initialization files like .profile, .bash_profile or .zprofile, depending on the system and shell configuration) can be used to run scripts when a user logs in, but this is not the same as setting up a service with systemd to run at system boot. The file is executed for interactive non-login shells, while or (for bash) is executed for login shells.
myapp.service
nano
vim
sudo nano /etc/systemd/system/myapp.service
Define the Service
Fill the file with the necessary service configuration. Here is a basic template:
Modify /path/to/your/binary_or_script to the full path of your binary or script. If necessary, specify the actual user and group under which the service should run instead of <username> and <groupname>. Adjust the Restart= policy and the RestartSec= as needed for your application.
Reload Systemd Manager Configuration
After saving your service file, reload the systemd manager configuration to recognize your new or changed units:
sudo systemctl daemon-reload
Enable the Service
Enable the service to start it on boot:
sudo systemctl enable myapp.service
Start the Service
You can start the service immediately without restarting:
sudo systemctl start myapp.service
Check the Service Status
To check if your service is running, use:
sudo systemctl status myapp.service
View Logs
If you encounter any issues, check the logs for your service with:
sudo journalctl -u myapp.service
Optional: Configure Dependencies
If your service depends on other services, like a database or a network service, you might want to modify the After= directive to specify the correct dependencies and order (e.g., mysqld.service or network-online.target).
After these steps, your binary or script should be configured to run at boot through systemd. Remember to thoroughly test your service to ensure it starts and operates as expected during boot and across reboots.
.bashrc
.bash_profile
.profile
Using .bashrc:
Keep in mind that placing your script or binary in .bashrc would mean it gets executed every time a user starts a new interactive shell session. This could be undesirable if the goal is to have the script running continuously in the background, as it could result in multiple instances of the script running at the same time. However, if you do decide to go this route, you would add a line to the end of the .bashrc file like so:
/path/to/your/binary_or_script &
The & at the end of the command puts it into the background.
Using .profile or .bash_profile:
Alternatively, by putting the script execution in .profile or .bash_profile, it will only be executed once when the user logs in. However, this is still not the same as system startup—it's user login—and won't be suitable for daemons or services that should be running regardless of whether a user is logged in or not.
Advantages of Systemd over .bashrc or other shell files for services:
Automatic startup: Services will start automatically at boot, while .bashrc needs a user to open a shell.
Service management: Systemd provides simple commands to start, stop, restart, and check the status of services.
Dependency management: Systemd allows you to specify the order in which services are started based on dependencies.
Logging and monitoring: Systemd handles logging and can restart services if they crash, based on the service unit configuration.
Multi-user systems: Systemd starts services before any user logs in, which is crucial for server environments that may not have users logged in all the time.
In conclusion, if you want your binary to run as a service at system startup, systemd is the recommended way to go. Using shell initialization files should generally be reserved for user-specific customization and environment settings, not for system-wide services.