12

what is the command or how would you make a process into a service in Linux? isn't a service basically a daemon?

6
  • 3
    Does this answer your question? How to make a program/process a service ?
    – muru
    Oct 10, 2021 at 12:59
  • 3
    @muru the title is similar but the question and answer is of traditional init. Oct 10, 2021 at 14:10
  • 3
    @Abdullah no reason why that can't be updated. There are plenty of questions here which have got answers with new technology. The question at its essence is the same after all.
    – muru
    Oct 10, 2021 at 14:35
  • @muru - it's a good point that you raise and one that is touched upon in this AU meta answer. The issue here is that the questions are distinct, although that isn't specified in their titles or body. This question is probably asking about systemd whilst the older one is probably asking about init, given the time difference. Either: (a) the titles should probably be changed to reflect this difference, or (b) the old question gets updated, but then that would mix obsolete info with current info. Not a simple conundrum to solve... :-( Oct 10, 2021 at 17:53
  • 5
    To add an insult to the injury, pre-systemd answers are not exactly "obsolete" since the other init systems are pretty much still in use in modern distributions.
    – fraxinus
    Oct 10, 2021 at 18:59

3 Answers 3

19

An example of a user service is the easiest way to describe how to do this.

Let's assume that you have a binary or a script, called mytask, that you want to run as a service, and it is located in /usr/local/bin/.

Create a systemd unit file, called my_example.service, in your home directory, ~/.config/systemd/user/, with the following contents:

[Unit]
Description=[My example task]

[Service]
Type=simple
StandardOutput=journal
ExecStart=/usr/local/bin/mytask

[Install]
WantedBy=default.target

The line ExecStart is the most relevant, as it is in this line that you specify the path to your binary or script that you want to run.

To make your service start automatically upon boot, run

systemctl --user enable my_example.service

If you want to start the service immediately, without rebooting, run

systemctl --user start my_example.service

If you want to stop the service, run

systemctl --user stop my_example.service

To check the status of your service, run

systemctl --user status my_example.service
5

In systemd terminology, service is a type of unit file, along with service, socket, device, mount, automount, swap, target, path, timer, slice, and scope. Yes, it is basically a way to run a system or user daemon. You can write your own. Read the official docs from above links and many tutorials are available in the internet.

4

There are a variety of ways to make a process a service in Linux. As others have touched on, you can use systemd to execute a process and watch its output, but depending on your language features, you can use the old-school method of the C 'double fork()' (python and some other languages have this too).

When you fork() in C, you create a child process. The parent process would actually keep a handle on this child process, but may not wait for it to complete. If the parent process finishes, the child process would be, quite literally, orphaned. fork()ing again means that init (process 1) adopts your new process.

Anyway, how does this all this fork()ing create a daemon? Well, the child processes continue running, even after the parent exits - which means it returns control to the shell that executed it. A basic example of fork() is below.

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

void forkexample()
{
  int ret = fork();
  if (ret == 0) {
    fork();
    /*
     * zomg i could run in here forever
     * as a daemon.
     * listen for input, monitor logfiles, whatever.
    */
    printf("I'm the child!\n");
    int x = 0;
    while (x < 10) {
      printf("Still running...\n");
      sleep(1);
      x++;
    }
  }
  else {
    printf("Child process spawned; pid %i\n", ret);
    printf("I'm a parent...\n");
  }
}
int main()
{
  forkexample();
  printf(" and I'm exiting.\n");
  return 0;
}

The output of running it will look like this: tada

Anyway, continuing on: the child process could continue running forever, as your program, instead. This is actually how I wrote one of my first real-world programs, controlling the fanspeed on my laptop.

2
  • 1
    This looks interesting but I can’t understand, from what you’ve written, why there need to be two forks. Part of the confusion is that when you talk about the “child” it’s unclear to me whether you mean the child or the grandchild.
    – jez
    Oct 11, 2021 at 21:28
  • @jez Strictly speaking, fork()ing only once will create the daemon/child process the OP is looking for. The double fork is sort of a relic of the way Unix processes are handled - see the excellent stackoverflow.com/a/5386753 for a better explanation than fits in a stack overflow comment box.
    – hlmtre
    Oct 12, 2021 at 0:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .