1

I have not found a way for systemd to recognize that some required non-systemd process is required to be running before I start mine. In Upstart I could do, for example,

if pgrep -f bin/postgres

The closest I've found is "path based activation", which would let me look for a lock file, but this is clunky and not guaranteed since sometimes lock files get orphaned.

I do not want to rewrite all the processes that are not using systemd so that they now do use it - I just want to start one particular process that way.

Is there no such option, or have I just not found it?

  • Read the manual, you can learn to write a service file. There's no such concept called non-systemd process – 炸鱼薯条德里克 Jun 19 at 5:50
  • 1
    @炸鱼薯条德里克 Obviously I will be writing a service file for my own application. I have spent many hours and looked at literally dozens of web pages that discuss how to use systemd. Not one of them has given me any clues on how to reference a process which is not under the direct and initial control of systemd. It sounds like you are telling me that all processes MUST and DO use systemd. I explained that I want to make minimal changes to the system, and asked if this was possible. If the answer is "No it is no possible", please say so if you know. Otherwise, clues beyond "RTFM" are appreciated. – mickeyf Jun 19 at 12:01
  • Systemd manage SERVICES not PROCESSES. "a process under the direct and initial control of systemd" "a process use systemd" Don't say none phrases you can't give exact definition. If you want to test if some program is running and decide whether to start your app, try pgrep in ExecStarPre option. Or ConditionXXX option, or make a script warper and do your own check-then-start logic. – 炸鱼薯条德里克 Jun 19 at 15:55
  • From man7.org/linux/man-pages/man5/systemd.service.5.html : "A unit configuration file whose name ends in .service encodes information about a process controlled and supervised by systemd." Clearly, some processes are controlled by systemd, but some may not be. Systemd manages services, and many services manage processes. Thank you for pointing out ExecStartPre, and of course I can write scripts to do whatever systemd does not, but that does not answer my question about what systemd does or does not do itself. – mickeyf Jun 19 at 17:27
  • Systemd provides so much functionality that you can easily make almost any program started as a service. Including your script. You write services for existing programs so they can be started as systemd services. Systemd cares about the state of a service, not a process, nor what code it's currently executing. – 炸鱼薯条德里克 Jun 20 at 3:03
1

The best option I can think of is the one that 炸鱼薯条德里克 mentioned in a comment: ExecStartPre:

# ...
[Service]
ExecStartPre=/usr/bin/pgrep -f bin/postgres
# ...

This honors the instruction that:

ExecStartPre= may not be used to start long-running processes

... because pgrep exits when it's done. It also achieves the desired functionality by allowing the service to start if the listed process exists and prevents the start of the service when the listed process does not exist.

If you attempt to start the service manually when the requisite process does not exist, you get:

Job for demo.service failed because the control process exited with error code. See "systemctl status demo.service" and "journalctl -xe" for details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.