How to start a process automatically after every Linux server reboot


Background :
     After all tiring days of coding,testing ,tuning ,  One would be eagerly  waiting to see his/her application  deployed onto the server and letting it run in the background . Having end user access the application, monitor the logs from backend. Everything looks good up until the server gets rebooted. The moment when the server goes for a reboot as part of planned server farm maintenance or a too many open files/Out Of Memory ,network contention and lot other reasons , that is when your application gets killed and doesn't come up until we manually start the app.
Motivation: 
   We can't afford to wait until end user complains about the app being not reachable. As a best practice , put the app to auto start upon server start up .
 I have covered auto respawner when accidentally the process gets killed in a different article. here  https://github.com/rockssk/bash_scripts/blob/master/respawner.sh
    We're focusing on some custom apps that are not managed through middleware .

Example : 

  I'm running my application in a docker container with container ID ba7bd07ff0ee 
[suresh@dockerhost~]$ docker ps
CONTAINER ID        IMAGE                                                                      COMMAND                  CREATED             STATUS              PORTS                    NAMES
ba7bd07ff0ee        docker-hub:1000/test_docker_image/test_docker_v1   "/bin/sh -c /startnot"   2 weeks ago         Up 29 hours         0.0.0.0:8888->8888/tcp   test_container_1
I created a script called start_myapp_container

[suresh@dockerhost~]$ cat start_myapp_container #!/bin/sh
docker start ba7bd07ff0ee
  I put the same file under /etc/init.d/ and set executable permission on it

chmod +x /etc/init.d/start_myapp_container
Just an additional check,  create a symlink to /etc/rc.d/
ln -s /etc/init.d/start_myapp_container /etc/rc.d/
As a note, always refer  absolute path of your script instead of a relative one, if there are any referred inside your script

Comments

Popular Posts