what is the command or how would you make a process into a service in Linux? isn't a service basically a daemon?
3 Answers
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
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.
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:
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.
-
1This 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.– jezOct 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.– hlmtreOct 12, 2021 at 0:40
systemd
whilst the older one is probably asking aboutinit
, 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... :-(