1

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

4

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.

  • 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 – R.W Aug 26 '16 at 18:43
  • @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. – grochmal Aug 26 '16 at 19:45
  • You will need to add an additional line to the service unit to have it run as user1 instead of root. – larsks Aug 27 '16 at 1:51
  • Thanks a lot larsks and grochmal. I just tried to copy + modify some stuff from an existing unit file (for nginx) .... and couldn't get my system to boot now :-) Luckily that was a pure test Centos 7 haha. But ya let me go with Systemd and see how. Unfortunately I have zero experience in scripting on Linux, but on Windows - kinda darn good ",) – R.W Aug 27 '16 at 8:31
  • 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! – larsks Aug 27 '16 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.