Run application/command as linux service with startup



        This post is about how you can run an application or a custom command as linux service. Here I will use a JAR (java application) to demonstrate the same as I require my java application to run as service with startup. To run java application as service, we need to make some shell script to start stop our command as service. Following are some simple steps and scripts to achieve our goal.


Steps:

(Note: Please set appropriate file permission for scripts, many times it causes problems.)

#1 Service starting script

/usr/local/bin/git_monitor-start.sh
#!/bin/bash

red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`

# Here 'git-monitor-0.0.1-SNAPSHOT.jar' is name of my java application

# Logic
# Get pid of already running service by application name
# If application/command is running then display error message that service is already running
# Else main logic to run application

pid=`ps aux | grep git-monitor-0.0.1-SNAPSHOT | grep jar | awk '{print $2}'`

if [ -n "$pid" ]; then
    echo "${red}[NOT OK] Service is already running!${reset}"
else
    cd /home/myuser/git
    nohup java -jar git-monitor-0.0.1-SNAPSHOT.jar &
    echo "${green}[OK] Service is booting now!${reset}"
fi

#2 Service stopping script

/usr/local/bin/git_monitor-stop.sh
#!/bin/bash

red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`

# Here 'git-monitor-0.0.1-SNAPSHOT.jar' is name of my java application

# Logic
# Get pid of already running service by application name
# If application/command is running then kill pid and display success message
# Else display error message that service is not running

pid=`ps aux | grep git-monitor-0.0.1-SNAPSHOT | grep jar | awk '{print $2}'`

if [ -n "$pid" ]; then
    kill -9 $pid
    echo "${green}[OK] Service is stopped!${reset}"
else
    echo "${red}[NOT OK] Service is not running!${reset}"
fi


#3 Service script

Create the following script (git-monitor) and put it on /etc/init.d

/etc/init.d/git-monitor
#!/bin/bash

# Use name of start stop script name in cases
# For restart case first use stop script then start script

case $1 in
    start)
        /bin/bash /usr/local/bin/git_monitor-start.sh
    ;;
    stop)
        /bin/bash /usr/local/bin/git_monitor-stop.sh
    ;;
    restart)
        /bin/bash /usr/local/bin/git_monitor-stop.sh
        /bin/bash /usr/local/bin/git_monitor-start.sh
    ;;
esac
exit 0

#4 Service is ready to run

Now we can use service command from command prompt.
$ service git-monitor start

$ service git-monitor stop

$ service git-monitor restart

#5 Start service with system

Put the script to start with the system (using SysV). Just run following command as root that's it:
# update-rc.d git-monitor defaults




Hope this information will help you.
I would really appreciate your feedback, suggestions, requests and ideas.

Thank You.


38 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

    rpa training in velachery| rpa training in tambaram |rpa training in sholinganallur | rpa training in annanagar| rpa training in kalyannagar

    ReplyDelete
  4. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
    Data Science course in kalyan nagar | Data Science course in OMR
    Data Science course in chennai | Data science course in velachery
    Data science course in jaya nagar | Data science training in tambaram

    ReplyDelete
  5. Just stumbled across your blog and was instantly amazed with all the useful information that is on it. Great post, just what i was looking for and i am looking forward to reading your other posts soon!
    Python training in marathahalli | Python training institute in pune

    ReplyDelete
  6. Thanks for your great and helpful presentation I like your good service.I always appreciate your post.That is very interesting I love reading and I am always searching for informative information like this.Well written article Thank You for Sharing with Us pmp training in velachery | project management certfication in chennai | project management training institute in chennai | pmp training fee | pmp certification course in chennai

    ReplyDelete
  7. This company will always be there to withdraw your web money, perfect money and bitcoin fund through bitcoin atm card . This company not only facilitates you with its services but also understands the stress that you go through while exchanging your e-money.

    ReplyDelete
  8. Thanks For sharing Your information The Information Shared Is Very Valuable Please Keep updating Us Time Just Went On Redaing The Article Python Online Course Devops Online Course Data Science Online Course Aws Science Online Course

    ReplyDelete
  9. Great article!!
    Thanks for sharing this post!
    VMware Training in Bangalore

    ReplyDelete
  10. Yeah!! MsgClub is a known marketing service provider that is based in Indore. Through the experience of more than a decade and expert business analytics into the picture, the institution understands various challenges faced by startup ventures and thus bring in a wide range of exclusive services for startup ventures under the umbrella of MsgClub for Startups.

    ReplyDelete
  11. Your info is really amazing with impressive content..Excellent blog with informative concept. Really I feel happy to see this useful blog, Thanks for sharing such a nice blog..
    If you are looking for any python Related information please visit our website Python classes in pune page!


    ReplyDelete
  12. I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article.
    ict based solutions bahrain

    ReplyDelete
  13. nice article...We are providing best training to students.internship in chennai for it training will be offered by different technologies. internship training can be useful for higher studies of school students, college and polytechnic students. Internship training will improve future career growth.

    ReplyDelete
  14. This blog resolved all my queries I had in my mind. Really helpful and supportive subject matter written in all the points. Hard to find such kind of blogs as descriptive and accountable to your doubts.cursus medium

    ReplyDelete
  15. Best post..
    Thanks for sharing with us,
    We are again come on your website,
    Thanks and good day,
    If you need any logo then,
    Please visit our site,
    buylogo

    ReplyDelete
  16. Thanks For Sharing Great Article We have shared article on Best 4K Gaming Monitors

    ReplyDelete
  17. Svu University B.Com 1st,3rd,5th Sem Result 2020
    Svu University B.Com 2nd,4th,6th Sem Result 2020
    I just stumbled over this page and have to say - wow. The site is really good and kept up to date. Continue like

    ReplyDelete
  18. Check out the latest Bollywood news, new Hindi movie reviews, box office collection updates
    Bell Bottom Full Movie Download & Review
    The Kings Man Full Movie Download & Review

    ReplyDelete
  19. Very Nice article Sir...Best writer article Sir...
    Love You Sir Provide authentic information.

    ReplyDelete
  20. Nice Blog. Thanks for sharing with us. Such amazing information. computer monitoring software 2021

    ReplyDelete
  21. Java For Loop with Simple Example - Simple Way to Learn
    Read Now

    ReplyDelete
  22. Thanks for your sharing. Wish you good health to have more good and useful articles.
    From Dịch vụ IT in Ho Chi Minh City, Vietnam
    Follow me: https://bit.ly/3DSrEa0

    ReplyDelete
    Replies
    1. یکی از انواع ماوس، ماوس‌ های گیمینگ هستند که برای بازی، طراحی و ساخته شده‌اند.

      ویژگی های اصلی این ماوس‌ها ردیابی دقیق‌ تر و با سرعت عملکرد بالاتر است؛

      اما ویژگی‌ هایی مانند قابلیت شخصی‌ سازی مخصوص خرید موس گیمینگ، راحتی و نحوه کاربری آن نیز از ویژگی‌ های ارنده ی ماوس‌ های گیمینگ حرفه ای به شمار می‌روند.

       

      Delete
  23. Martial Arts Studio Software - JIBASoft's martial arts studio software and Dojo management software helps you in running your school hassle-free, it is simple to use and you can share your love of martial arts with more students.

    ReplyDelete
  24. Is merit casino legit? | The Cardroom Forum
    I've come septcasino across some games from 1xbet a lot of real money gambling software and i 메리트 카지노 want to see them. I like them and i love them. They have many titles too.

    ReplyDelete
  25. At PQube IT Service Provider, our in-depth knowledge of both business and technology will help to make your company’s organizational goals a reality. Your business is unique, and your technology solutions should be customized to fit its needs. Unlike other development companies, all of our employees are full-time and in-house. We don’t outsource. We are business consultants, programmers, marketing professionals, designers, and managers - one big happy family.

    ReplyDelete
  26. Data science is analyzing the raw data collected from different sources and making that data into useful information needed for the organization. It is vital to analyze a large amount of raw data into something meaningful and extract the valuable information necessary for the different purposes of the organization, such as marketing, sales, and decision-making. Join the Data Science Training in Chennai at FITA Academy to learn about data science.

    Data Science Training in Chennai

    ReplyDelete