Application as Service on Centos 7

6,397

To run something on startup on CentOS 7, Fedora, and recent versions of Ubuntu, you need to create a systemd unit. What you have in your question is a legacy init script and is no longer necessary.

A simple unit file might look like this:

[Service]
Type=simple
ExecStart=/bin/myservice

[Install]
WantedBy=multi-user.target

Assuming that /bin/myservice runs in the foreground, the above is really all you need. You would place this into /etc/systemd/system/myservice.service if you are just setting things up on your own system, or /usr/lib/systemd/system/myservice.service if you are building a package for distribution, and then:

  1. To start the service immediately: systemctl start myservice
  2. To mark the service for automatic start next time you boot: systemctl enable myservice

Tools like chkconfig and update-rc.d are legacy tools that are not necessary in environments running systemd.

Share:
6,397

Related videos on Youtube

R.W
Author by

R.W

Updated on September 18, 2022

Comments

  • R.W
    R.W over 1 year

    Pretty new to Centos 7 and most of my earlier experiences were with Ubuntu. I have just set up an application on Centos 7 and I saw on their product website that to run the application as a Linux Service (Start at System Startup), this is the script that one needs to use.

    #! /bin/sh
    export JAVA_HOME="/usr/lib/jvm/jdk1.7.0_07"
    
    startcmd='<PRODUCT_HOME>/bin/listener_mm.sh start > /dev/null &'
    restartcmd='<PRODUCT_HOME>/bin/listener_mm.sh restart > /dev/null &'
    stopcmd='<PRODUCT_HOME>/bin/listener_mm.sh stop > /dev/null &'
    
    case "$1" in
    start)
       echo "Starting the Listener Server ..."
       su -c "${startcmd}" user1
    ;;
    restart)
       echo "Re-starting the Listener Server ..."
       su -c "${restartcmd}" user1
    ;;
    stop)
       echo "Stopping the Listener Server ..."
       su -c "${stopcmd}" user1
    ;;
    *)
       echo "Usage: $0 {start|stop|restart}"
    exit 1
    esac
    

    Haha hope esac is not specific to other distro :?

    So I created the script, substituted the correct values as per my enviroment, and added a Symbolic link to my script. I have couple of questions 1) The above script is perhaps for Ubuntu or another Linux Distro. So do I need to make any changes to the script? 2) How do I make this run as a service , for all users? I would want this to start at System Startup. Debian / Ubuntu has update-rc.d, and I guess i need to use chkconfig for this. Can I have any pointers on this? It happens to be a machine with few other things, so I am a little vary of experimenting and if anyone with experience on this could give me some pointers, it will be great.

    I am doing some reading on systemd but any help will be really appreciated.

    Thanks a ton.

    Cheers, M.M

  • R.W
    R.W over 7 years
    Thanks a lot larsks. I definitely am not building a package for distribution. I just need to be able to do exactly the same task that the above script does - Start, Stop and restart. Probably your unit file example is precisely what I need. I guess it should do what the script in my OP does? I am a total newbie to Centos 7 scripting / systemd etc :) Thanks once again
  • grochmal
    grochmal over 7 years
    @MenonM - This is absolutely correct, systemd is the way to go in CentOS 7, pretty much all major distros switched to systemd in their latest release. Doing the service start, stop and restart; just calls systemctl behind the scenes on CentOS 7.
  • larsks
    larsks over 7 years
    You will need to add an additional line to the service unit to have it run as user1 instead of root.
  • larsks
    larsks over 7 years
    MenonM, I've given you pretty much everything you need in this answer, except for setting the user, which I can think you can figure out from reading documentation. You don't need to go modifying anything more complicated. Start simple!