Notes about Background Commands

Notes about Background Commands; run multiple commands in docker-compose

Posted on Sat, 08/04/2018 - 11:07 by zhilevan

I've written an article about the running Php scripts in the background   two years ago, Now I'm going to write an advanced version of it, nowadays I'm working with docker and micro-services which we need in our architecture.

as I explained before

 

What is nohup?

Most of the time you log-in into a remote server via ssh. If you start a shell script or command and you exit (abort remote connection), the process / command will get killed. Sometime job or command takes a long time. If you are not sure when the job will finish, then it is better to leave job running in background. But, if you log out of the system, the job will be stopped and terminated by your shell.

What do you do to keep the job running in the background when the process gets SIGHUP?

The answer is simple, use nohup command line utility which allows to run command/process or shell script that can continue running in the background after you log out from a shell.

 

The first solution for running a command in the background is 

nohup bash -c "COMMAND 2>&1 &"

 

But sometimes it doesn't work.it will run the next step as soon as it finishes. In many cases the server you started isn’t yet fully ready. To allow the server a bit more time to get ready add a sleep command. How large the argument needs sleep to depend on the service you start and you probably need to tweak it.

nohup bash -c "COMMAND 2>&1 &" && sleep 6

Acutally, this command help me while I was trying to run a command in docker-compose

 

first tried was 

nohup bash -c "/usr/local/bin/scrapyd 2>&1 &"
python manage.py makemigrations && python manage.py migrate && gunicorn $project_name.wsgi -b 0.0.0.0:8000

but scrapyd  daemon not got run, I tried several solutions but sleep 6 save my time. and the final solution is 

 

nohup bash -c "/usr/local/bin/scrapyd 2>&1 &" && sleep 6
python manage.py makemigrations && python manage.py migrate && gunicorn $project_name.wsgi -b 0.0.0.0:8000

 

the entry point for my micro-service  was  in docker-compose.yml file:

command: bash -c "./run_django.sh"

and the shell file contains

 

#!/bin/sh
project_name="webservice"

if [ ! -d $project_name ]; then
    django-admin startproject $project_name
fi

cd $project_name

nohup bash -c "/usr/local/bin/scrapyd 2>&1 &" && sleep 6
python manage.py makemigrations && python manage.py migrate && gunicorn $project_name.wsgi -b 0.0.0.0:8000

 

Now we have two running command in docker-compose.