InsaneCoder
InsaneCoder

Reputation: 8268

Linux : Start a a service after a particular service has started on boot

I have two services A and B which I want to start on boot. But A should start first and then only B should start.

I enabled the services using systemctl enable service_name.

Now the services are starting but not in order i.e B is starting before A. Is there any way I can configure their start order?

Upvotes: 0

Views: 182

Answers (2)

Carl
Carl

Reputation: 51

You can add the following command at the end of the startup script of A, and disable B to be started on bootup: systemctl start B

Upvotes: 1

Zeiss Ikon
Zeiss Ikon

Reputation: 481

They're starting out of order because Linux uses "makefile style concurrent boot" during startup -- and the A process is taking longer to start than the B process. The simplest way to delay process B is with a sleep command -- a few seconds is likely enough -- though this will delay the completion of startup by a fixed amount (and, if process A takes a variable time to start, as with opening a wifi connection etc., this may not always work unless you set the time higher than it usually needs to be).

More reliable, and possibly less delay in startup, would be to use something like lsproc | grep proc_a | wc -l to check for existence of process A (or a child of A) as a condition for starting process B -- put this in a short loop with a 1 or 2 second sleep (so it doesn't hog all your CPU while it waits) and it'll effectively keep B back until A is running, without unnecessary delay.

Upvotes: 0

Related Questions