Score:0

How to run a process/binary on System startup/reboot in Ubuntu 20.04 LTS?

us flag

I have an Ubuntu Server 20.04 LTS, and a binary file to run a process inside folder /home/dir1/dir2 called binary.

  1. I believe there might be more than one methods to this but, if I could possibly know please, where and how is (are) the best way(s) to config so this binary/process so it can run automatically upon every time the system restarts?

  2. Is there any way to ensure to keep this process/binary keeps running at all time? Meaning, the system keeps checking/monitoring this process, and if it gets killed or stopped manually, or by user or system error etc, it is restarted/re-run automatically?

Any suggestions with config examples would be much appreciated.

Cheers

Score:0
vn flag

Systemd can to this for you.

Here is an example of a typical unit file:

$ cat /etc/systemd/system/yourdaemon.service
[Unit]
Description=Your Daemon
After=network-online.target
Wants=network-online.target systemd-networkd-wait-online.service

[Service]
ExecStart=/path/to/daemon

[Install]
WantedBy=multi-user.target

And here is a unit file that has restart on failure enabled:

$ cat /etc/systemd/system/yourdaemon.service
[Unit]
Description=Your Daemon
After=network-online.target
Wants=network-online.target systemd-networkd-wait-online.service

StartLimitIntervalSec=500
StartLimitBurst=5

[Service]
Restart=on-failure
RestartSec=5s

ExecStart=/path/to/daemon

[Install]
WantedBy=multi-user.target

I think it's beyond the scope of this answer to go through everything in detail, but in short:

  1. Create a unit file for the service (binary/process) you want to start.
  2. Make sure to include the StartLimit and Restart options as indicated above.

Reference.

billyhanks1 avatar
us flag
Thanks mate, the 2nd unit did both for me. You're a gem. Cheers
venkatvb avatar
cn flag
For me, in EC2 AL2 instance - I had to enable the service via `systemctl`. `sudo systemctl enable yourdaemon`. You could check if it's enabled by `sudo systemctl is-enabled yourdaemon`
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.