Daemonize certain processes -> Stuck with script

All,

This weekend I was working on setting up my XBMC server. To do this is need to run serveral programs, but I came to the conclusion that init is changed to systemd.
Not a problem you should say, however with init you could create daemon processes, which I could not find with systemd.
Solution was to build my own Daemonizer script per program where needed (since the programs I wanted to don’t have PID).

So after searching I found the folowing script:
How to Daemonize in Linux [Doug Potter]

Problem is that I don’t see how to use it. Of course I did fill in the daemon name and run as user.

After that I can compile with:
gcc -Wall daemon_script.c -o deamon

Which can be executed, but there doesn’t happen a thing, so probably I do something wrong.
Please help me out, read complete artikel on the website but doesn’t give me a clue?

Update:
Oke, working a little however a PID in /var/run is still missing and I get an error on:
“/var/lock/subsys/”

Because that folder does not exist on OpenSuSe 12.1. Hoe should this be adjusted to make it working.
Furthermore, how do I get a PID.

On 2012-06-03 13:56, TUDStudent wrote:
>
> All,
>
> This weekend I was working on setting up my XBMC server. To do this is
> need to run serveral programs, but I came to the conclusion that init is
> changed to systemd.
> Not a problem you should say, however with init you could create daemon
> processes, which I could not find with systemd.

I’m sure you can launch with systemd any program as was done with systemv -
but don’t ask me how.

Being a daemon or not is a secondary thing.


Cheers / Saludos,

Carlos E. R.
(from 11.4 x86_64 “Celadon” at Telcontar)

Hello, writing systemd unit files is pretty simple, it’s more straight forward than for the good old init. I will demonstrate it on a short example:

  [Unit]  Description=My cool daemon  After=mysql.service    [Service]  User=daemon_user  ExecStart=/usr/local/bin/daemon    [Install] WantedBy=multi-user.target  

If your daemon depends on some other processes you should add it to After section. Maybe you also want to replace daemon_user with the your spcific daemon user. Then copy this file to /etc/systemd/system/daemon.service, reload configuration with

 systemctl --system daemon-reload 

and start it with systemctl start daemon.service that’s it. To enable it on startup just type systemctl enable daemon.service. It’s really quite simple, isn’t it :slight_smile: Hope this helps

I am afraid that @Monex, while using CODE tags, did not realy copied/pasted directly from the terminal into the post. Thus the Line feeds are gone. Can you please repost or this this realy as it should be?

Hello,

in preview it looked quite good :stuck_out_tongue:

Here the version with corrected code tags :slight_smile:

writing systemd unit files is pretty simple, it’s more straight forward than for the good old init. I will demonstrate it on a short example:

 
[Unit] 
Description=My cool daemon 
After=mysql.service  

[Service] 
User=daemon_user 
ExecStart=/usr/local/bin/daemon  

[Install] WantedBy=multi-user.target 

If your daemon depends on some other processes you should add it to After section. Maybe you also want to replace daemon_user with the your spcific daemon user.

Then copy this file to /etc/systemd/system/daemon.service, reload configuration with

 systemctl --system daemon-reload 

and start it with systemctl start daemon.service that’s it.

To enable it on startup just type systemctl enable daemon.service. It’s really quite simple, isn’t it

Hope this helps

Thanks for the repost.

It realy is simple and it is shown here earlier. But I allways forget where to find it.:frowning:
Will now make a copy of your example in a file here. Hope I will remember I have that file somewhere when tiimes comes :wink:

Jun 3 22:00:18 linux-7ffb systemd[1608]: Failed at step EXEC spawning /usr/local/bin/daemon: No such file or directory
Jun 3 22:00:18 linux-7ffb systemd[1]: daemon.service: main process exited, code=exited, status=203
Jun 3 22:00:18 linux-7ffb systemd[1]: Unit daemon.service entered failed state.

Getting the above error in my messages. Do I mis an RPM to get this working, or is it located somewhere else.
(I really do not have this folder/file -> Opensuse 12.1)

Don’t you understand that you have to fill in your parameters? E.g. for the example of /usr/local/bin/deamon you should of course replace it wiith the path to your own script, wherever it may be. And of course all the others. I would not believe you also use a name as “My cool deamon”???

Ha such an idea, but then I understand why you gave me this solution (i already have an .service file which forks).

However the program I run, in this case “newcs”, which is a cardsharing server is not 100% deamon and does not store its pid in /var/run

Therefore I am having problems with running it as forking service.
Do you have a solution to this?

This is the reason I wanted to build my own daemon script.

To be honest, I cannot match the various statements you’re posting. But it does raise a question: have you been trying to do this on WIndows?

To be a service or a daemon, is not determined by whether there’s a pid file in /var/run. Take a look at the existing .service files, at some of the scripts in /etc/init.d

Consider a “while true do " construction, run it in the background by adding " &” to the script/command.