Start program as service linux

How to Run Script or Program at Startup with Systemd on Linux

There are plenty of ways to automatically run a script or command at Linux boot up and after reboot, either by using cron , rc.local or my preferred method — systemd . It creates services which are easier to manage. You can start/stop/enable/disable them with systemctl after they’re set up. Furthermore, services can even restart themselves after startup failure or crash.

Create Service

Create the unit configuration file /etc/systemd/system/your-service.service with the text editor of your choice:

    Vim (command-line editor for advanced user)

$ sudo vim /etc/systemd/system/your-service.service 
$ sudo nano /etc/systemd/system/your-service.service 
$ sudo touch /etc/systemd/system/your-service.service $ gedit admin:/etc/systemd/system/your-service.service 

Then add the following lines to your-service.service :

/etc/systemd/system/your-service.service[Unit] Description=YourService [Service] ExecStart=/path/to/your/script.sh [Install] WantedBy=multi-user.target Plain text

Service example

Auto Restart (Optional)

To tell the service to restart itself after failure, append Restart=always under the Service section:

/etc/systemd/system/your-service.service[Service] . Restart=always Plain text

Acceptable values of the Restart directive include no , on-success , on-failure , on-abnormal , on-watchdog , on-abort and always .

It’s also a good idea to set RestartSec to configure the amount of time to sleep before restarting the service.

/etc/systemd/system/your-service.service[Service] . RestartSec=5 Plain text

Set Environment (Optional)

To set the environment variables:

/etc/systemd/system/your-service.service[Service] . Environment="production=true" Plain text

Set Working Directory (Optional)

/etc/systemd/system/your-service.service[Service] . WorkingDirectory=/home/user Plain text

Enable Service

Before enabling your service, change the permissions of your-service.service to 644 :

$ sudo chmod 644 /etc/systemd/system/your-service.service 

Remember to reload the daemon after the systemd unit files are changed:

$ sudo systemctl daemon-reload 

Then enable the service so it will run at system startup:

$ sudo systemctl enable your-service 

Verify It’s Working

To verify the service is working, start the service and check its status:

$ sudo systemctl start your-service $ systemctl status your-service 
○ greet.service - Greet Loaded: loaded (/etc/systemd/system/greet.service; enabled; vendor preset: disabled) Active: inactive (dead) since Thu 2022-02-17 15:12:19 CST; 3s ago Process: 7810 ExecStart=/greet.sh (code=exited, status=0/SUCCESS) Main PID: 7810 (code=exited, status=0/SUCCESS) CPU: 3ms Feb 17 15:12:19 linux systemd[1]: Started Greet. Feb 17 15:12:19 linux greet.sh[7810]: Hello World Feb 17 15:12:19 linux systemd[1]: greet.service: Deactivated successfully. Output

Since mine is a simple echo «Hello World» script, it’ll be considered as inactive once executed.

Disable Service

To disable the service if you don’t want it to run at system startup anymore:

$ sudo systemctl diable your-service 

Stop Immediately

Disabled services can still be running in the background, to stop the service immediately:

$ sudo systemctl stop your-service 

Comments

Preview of Configure PPPoE DSL Connection on Arch Linux/Manjaro with pppoe-setup

Configure PPPoE DSL Connection on Arch Linux/Manjaro with pppoe-setup

Most ISPs (Internet Service Providers) offer multiple dynamic IP addresses and even come with a static one. To take advantage of that, we’ll be setting up a PPPoE (Point-to-Point Protocol over Ethernet) DSL (Digital Subscriber Line) connection on our own machine to obtain an independent IP address.

Читайте также:  Using screen on linux

Preview of Two Ways to Install Packages from AUR on Arch Linux/Manjaro

Two Ways to Install Packages from AUR on Arch Linux/Manjaro

Just like Debian/Ubuntu’s package manager apt, Arch Linux/Manjaro has its own package manager called pacman to help you install packages. But the story doesn’t end there. On Arch-based Linux you can even install community-maintained packages from AUR (Arch User Repository), which is a lot like PPA (Personal Package Archive) of Ubuntu.

Preview of Auto-Renew Let's Encrypt Free Certificates for Nginx on Linux

Auto-Renew Let’s Encrypt Free Certificates for Nginx on Linux

More than 75% of all websites use HTTPS nowadays. Serving content over HTTPS is not only good practice for search engine optimization (SEO), but essential for security reasons, especially for those requests containing sensitive information.

Preview of Create/Remove/Resize Swap Space without Modifying Partitions on Linux

Create/Remove/Resize Swap Space without Modifying Partitions on Linux

When a Linux machine runs out of physical memory (a.k.a random-access memory — RAM), the swap space will be used instead. It literally swaps data from your RAM to your disk space to free up memory. In some perspectives, it even speeds up your CPU too, since when the less the memory is free, more time the CPU spend on GC (Garbage Collection).

Источник

How to run a program as a service (silent)?

I have a python based server which i start from the terminal. This particular instance of the terminal then gives control to the program, and the program uses it as a kind of logging window, until its closed. Is this normal, or should i somehow try to start the program some other way in which it will simply show as an active process? If i close the terminal from which i started the program, the program dies with it. Thank you

Читайте также:  Steam engine for linux

PHP is mentioned in this answer but it applies to Python, too: askubuntu.com/questions/26555/running-php-cli-server/…

5 Answers 5

Turn it to a daemon (service)
daemon —name=»yourservicename» —output=log.txt sh yourscript.sh

Even old bash is using & for sending processes to background, but there is few other ways too .. but basic two are these :

1.)$~ your_command > outputfile_for_stdout & # runs your command in background, giving you only PID so you can exit that process by `kill -9 PID_of_process` # & goes at the end of row 2.)$~ your_command > outputfile_for_stdout # this will run your program normally # press Ctrl + Z then program will pause $~ bg # now your program is running in background $~ fg # now your program came back to foreground 3.)you can run terminal window under screen command so it will live until you either kill it or you reboot your machine $~ screen $~ run_all_your_commands # Ctrl + A + D will then detach this screen $~ screen -r will reattach it 

Some other useful commands :

 $~ jobs # will show you all processes running right now, but without PID $~ ps # will show you all processes for actual terminal window 

Источник

Оцените статью
Adblock
detector