Search code examples
linuxrpm-specautostartsystemctl

How to autostart Linux service after reboot if a condition is met?


How can I please auto start a Linux service after system reboot, but only if a certain file exists?

This is what I have so far, which does not auto-start the service on reboot:

In the MyService.spec file:

systemctl enable MyService
if [[ -f $FILE ]]; then
    systemctl start MyService
fi

I have also created the MyService.service file which correctly contains the ExecStart directive.


Solution

  • change ExecStart in MyService.service to be conditional with bash. for example:

    ExecStart=/bin/bash -c '[ -f file_path ] && MyService_Binary'

    It will start the service if file_path exists.