0

This question already has an answer here:

In CentOS 7 I wanted to start httpd service only in runlevel 3 and 5, in other runlevels such as 2 and 4 the service shouldn't start.

In CentOS 6 we can achieve this by using "chkconfig" command. Looking for similar solution in CentOS 7 environment.

marked as duplicate by thrig, Satō Katsura, DarkHeart, strugee, Anthon May 14 '17 at 16:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Thanks Thomas. But how to force httpd to run only on runlevel 3 and 5. What I am looking is when I switch runlevel to 2 the httpd service should stop. – user358804 May 12 '17 at 18:37
  • @user358804 RHEL7 is based on systemd. systemd doesn't really have a concept of runlevels (it has targets which are sort of a superset of the functionality provided by runlevels). so your question of "how do I force httpd to run only on such-and-such runlevels under systemd" is somewhat nonsensical. you really should read that question; it was linked for a reason. – strugee May 14 '17 at 3:32
0

I see that you have mentioned that you would like to start HTTPD in runlevel 3 and 5, and to stop HTTPD in runlevel 2 and 4. Here is a URL to the Red Hat manual the provides a great overview of Init and SystemD.

https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/sect-Managing_Services_with_systemd-Targets.html

The Red Hat manual states that multi-user.target is the same as runlevels 2, 3, and 4.

  • Runlevel 0 = poweroff.target
  • Runlevel 1 = rescue.target
  • Runlevel 2 = multi-user.target
  • Runlevel 3 = multi-user.target
  • Runlevel 4 = multi-user.target
  • Runlevel 5 = graphical.target
  • Runlevel 6 = reboot.target

With this in mind, it seems like there would be no point in using a command such as systemctl set-default multi-user.target in an attempt to switch between runlevels 2, 3, and 4, since runlevels 2, 3, and 4 are synonymous with multi-user.target.

It is noteworthy that the systemctl cat httpd command can be used to view some of the configuration information. Related to your question, this command will show that HTTPD is set to multi-user.target.

# systemctl cat httpd
. . .
[Install]
WantedBy=multi-user.target

I am not sure if this would be appropriate for your environment. A possible solution to your need may be to have a script that determines the current run-level of the system, and then uses some if loops to start or stop the HTTPD daemon based on the target. For example, perhaps the script could look a little something like this.

#!/bin/bash
target=`systemctl get-default`
if [ $target == "multi-user.target" ]
then
  systemctl stop httpd
else
  systemctl start httpd
fi

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