Search code examples
linuxsynchronizationbackground-process

Synchronization issue with running linux commands in the background


I have to run three blocking linux commands(command1, command2 & command3) and I implemented synchronization so that if user runs command2 using terminal2 while command1 is executing in terminal1 my server returns an error saying command1 is still executing. Same for command3. But If I do a loop like below

for i in {1..3}; do { command$i & }; done

All three start executing parallel which is undesired. Is it a synchronization fault or I shouldn't run those commands this way? Is running commands in the background not same as running them in different terminals?

I feel I am missing something obvious here. Please enlighten me.


Solution

  • The ampersand is putting the jobs in the background, which returns immediately and has the effect of running them in parallel. To run them in series, remove the &.