Linux start process as service

Linux: process into a service

There is no single way to do this: it all depends on the particular Linux distribution and the service management daemon that comes with it. The major ones are System V, upstart, and systemd by the timeline. Choose the one that matches your distribution best. Here’s the tutorial: digitalocean.com/community/tutorials/…

3 Answers 3

That depends on your system manager

the most common way to do that on debian/ubuntu is to build an initscript and place it in /etc/init.d or /etc/rc/init.d and place a script named mytestprogram in that.

this is an example initscript:

#!/bin/sh ### BEGIN INIT INFO # Provides: testone # Required-Start: $local_fs # Required-Stop: $local_fs # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # X-Interactive: false # Short-Description: Example init script # Description: Start/stop an example script ### END INIT INFO DESC="test script" NAME=testone #DAEMON= do_start() < echo "starting!"; >do_stop() < echo "stopping!" >case "$1" in start) do_start ;; stop) do_stop ;; esac exit 0 

I suggest you to look some scripts in that directory, It’s simple if you know bash a little 😉

Are the comments in the first few lines necessary ? (ex. # Required-Stop: $java ) is there some parsing of comments ? Or, is this a good practice that everyone is following and code works fine without the comments ?

Do we need to restart the machine to take this script into effect? I created a script like one mentioned, but couldn’t use it immediately.

Here is a sample shell script (make sure you replace the MAT name with the name of the your application):

I create one GitHubGist with the latest version of my script and a brief explanation to help those who need it. GitHub Gist link

#!/bin/bash ### BEGIN INIT INFO # Provides: MATH # Required-Start: $java # Required-Stop: $java # Short-Description: Start and stop MATH service. # Description: - # Date-Creation: - # Date-Last-Modification: - # Author: - ### END INIT INFO # Variables PGREP=/usr/bin/pgrep JAVA=/usr/bin/java ZERO=0 # Start the MATH start() < echo "Starting MATH. " #Verify if the service is running $PGREP -f MATH >/dev/null VERIFIER=$? if [ $ZERO = $VERIFIER ] then echo "The service is already running" else #Run the jar file MATH service $JAVA -jar /opt/MATH/MATH.jar > /dev/null 2>&1 & #sleep time before the service verification sleep 10 #Verify if the service is running $PGREP -f MATH > /dev/null VERIFIER=$? if [ $ZERO = $VERIFIER ] then echo "Service was successfully started" else echo "Failed to start service" fi fi echo > # Stop the MATH stop() < echo "Stopping MATH. " #Verify if the service is running $PGREP -f MATH >/dev/null VERIFIER=$? if [ $ZERO = $VERIFIER ] then #Kill the pid of java with the service name kill -9 $($PGREP -f MATH) #Sleep time before the service verification sleep 10 #Verify if the service is running $PGREP -f MATH > /dev/null VERIFIER=$? if [ $ZERO = $VERIFIER ] then echo "Failed to stop service" else echo "Service was successfully stopped" fi else echo "The service is already stopped" fi echo > # Verify the status of MATH status() < echo "Checking status of MATH. " #Verify if the service is running $PGREP -f MATH >/dev/null VERIFIER=$? if [ $ZERO = $VERIFIER ] then echo "Service is running" else echo "Service is stopped" fi echo > # Main logic case "$1" in start) start ;; stop) stop ;; status) status ;; restart|reload) stop start ;; *) echo $"Usage: $0 " exit 1 esac exit 0 

Источник

How can I make an executable run as a service?

Ubuntu 18.04 uses systemd. The scripts in /etc/init.d are only used if a systemd unit file does not exist for the service. It is preferable that systemd services do not daemonize.

You have 2 questions here, and the answer to one does not necessarily dictate the answer to the other. A linux OS may have multiple daemon managers.

2 Answers 2

On Ubuntu 18.04, [. ]

I heard there are several ways of managing services: system V init, systemd, upstart, . Which one am I using?

You’re using systemd, that’s the init that’s shipped on Ubuntu 18.04. (Also on Ubuntu 16.04, on Fedora, on openSUSE, on Arch Linux, on RHEL 7, on CentOS 7, on CoreOS, and it’s also the default on Debian 9.)

One good way to confirm that you’re running systemd is to run the command systemctl . If it’s available and it produces output when run, then you’re running systemd.

On Ubuntu 18.04, I can start or stop some service by

sudo service cron start/stop 

I can list some services by

Please note that the service command shipped in some systemd distros is there mostly for backward compatibility. You should try to manage services using systemctl instead.

$ sudo systemctl start cron $ sudo systemctl stop cron $ systemctl status cron 

And you can find status of all units with a simple

The output matches the files under /etc/init.d/ .

That’s not necessarily the case with systemctl , since systemd native units are stored in /etc/systemd/system/ and /usr/lib/systemd/system/ .

systemd does include compatibility with old SysV init scripts (through systemd-sysv-generator, which creates a systemd native service unit calling the commands from the init script), so if you have init scripts under /etc/init.d/ , they’ll most likely show up in systemd as well.

Shall I use systemd instead of init on Ubuntu?

The term init generally refers to the first process run when the system boots, the process run with PID 1. systemd runs with PID 1, so by definition systemd is an init (and so was upstart before it, and SysV init as well.)

If you’re asking «should I use systemd instead of SysV init?», well then you’re already using systemd instead of SysV init, since you’re on Ubuntu 18.04. (And, as pointed out above, most distributions you’d pick these days would most likely include systemd as their init.)

Now, you could be asking «should I use systemd units instead of init scripts?» and that question is more relevant, since arguably you have a choice here where both options will work.

My recommendation here is that you should manage services using systemd units, which is the native mode of operation. Creating an init script simply adds a layer of indirection (since the generator will just create a systemd unit for you anyways.) Furthermore, writing systemd units is simpler than writing init scripts, since you don’t have to worry about properly daemonizing and scrubbing the environment before execution, since systemd does all that for you.

How can I make an arbitrary executable file (either ELF or shell script) become a service?

See the examples on the man page. The simplest example shows how easy it can be to create a service unit:

[Unit] Description=Foo [Service] ExecStart=/usr/sbin/foo-daemon [Install] WantedBy=multi-user.target 

Store this unit under /etc/systemd/system/foo.service , then reload systemd to read this unit file with:

$ sudo systemctl daemon-reload 
$ sudo systemctl start foo.service 

And enable it during startup with:

$ sudo systemctl enable foo.service 

You can check the status of the service with:

$ systemctl status foo.service 

Of course, systemd can do a lot more for you to manage services, so a typical systemd unit will be longer than this one (though not necessarily that much more complex.) Browse the units shipped with Ubuntu under /usr/lib/systemd/system/*.service to get a better picture of what’s typical, of what to expect.

No! Don’t run in background, don’t worry about process groups or sessions, etc. systemd takes care of all that for you. Just write your code to run in foreground and systemd will take care of the rest.

(If you have a service that runs in background, systemd can manage it, with Type=forking , but things are much easier when just running in foreground, so just do that if you’re starting a new service.)

This one is about applications using the «Spring Boot» Java framework. Unless you’re writing Java code and using that framework, it’s not relevant. If you’re writing Java code, try instead to just run your service in foreground instead.

The question is about upstart, the answer is about SysV init scripts. While SysV init scripts will work with systemd, it’s preferable that you write systemd units directly, as mentioned above.

So, no, I’d say neither of those are relevant.

I’d recommend trying to learn more about systemd service units instead.

This site is also a great resource for that, so feel free to post more questions about it as you explore writing your own systemd units for your services.

Источник

Читайте также:  Does my computer have linux
Оцените статью
Adblock
detector