1

As part of a provisioning script for CentOS 7 I am in need to have a one-liner that performs the following. Unfortunately, I have no clue how to achieve that.

  • If httpd is running then stop it
  • If httpd is not running then check if httpd is installed at all & start it
  • ideally the result is logged into /log/httpd/ AND /&hostname/log/httpd/

Anyone able to help?

  • 5
    why do you need a one-liner? is this a homework problem? – Skaperen Apr 15 '15 at 12:15
  • basically: I using a script that loads a yaml file containing the code to be run as part of cloud-init - the whole bunch then gets fired using the digital ocean API. Short: I try / need to avoid multi-lines in yaml as they get eaten to often. – frank Apr 15 '15 at 12:20
  • Isn't it just systemctl restart httpd.service? – ott-- Apr 15 '15 at 13:57
  • Define "one-liner". Does it need to be under a certain limit of characters or simply on a single line in a script file? The latter can be done by joining commands with ; or && instead of using newlines. – terdon Apr 15 '15 at 14:22
3

In CentOS7, you have systemctl that will pretty much do most of this for you. If Apache is installed via the standard packages, this should work for you out-of-the-box:

echo -n $(date +"%s %F %T"): \
if systemctl is-active httpd; then \
    systemctl stop httpd && echo "httpd stopped"; \
elif systemctl enable httpd; then \
    systemctl start httpd && echo "httpd started"; \
else \
    echo "httpd not installed"; false;\
fi 2>&1 || echo "Failure: $?" | \
 tee -a /var/log/httpd/status.log /some/other/location/log/httpd/status.log

I broke it into several lines for clarity. To collapse it to one line, remove the \'s and newlines. You can add more verbosity to the logging.

  • sweet! Thanks so much. I wonder why its so easy to understand if you see it written, but so hard figuring out how to achieve that without help. – frank Apr 15 '15 at 16:23
  • 1
    The script doesn't cover some unusual boundary cases: (1) is-active is true but httpd is actually hung and non-responsive so that stop() doesn't succeed (2) systemctl fails to start httpd. Either way, the exit code will be non-zero but you may not see why in the logs. Solve this with something like || echo "Unknown failure: $?" between the final fi and the tee. It's also useful to date-time stamp the logs. Do this by preceding the whole thing with echo -n $(date "+%s %F %T"): – Otheus Apr 15 '15 at 16:29
  • very good point - I am with you but it would take me a day to play around to achieve what you mentioned in the previous comment. So for my current purpose the provided script is perfect enough (also in terms of its logic and simplicity). – frank Apr 15 '15 at 16:34
1

Here is my onliner:

( if service httpd status; then echo 'stop'; service httpd stop; else echo 'check installed'; if ! rpm -qa | grep -q '^httpd-'; then echo 'install'; yum -y install httpd; fi; echo 'start'; service httpd start; fi ) &> /tmp/temp.log; cat /tmp/temp.log >> /log/httpd/some.log; cat /tmp/temp.log >> /&hostname/log/httpd/another.log; rm /tmp/temp.log
  • Thanks for taking the time putting it together - I started with yours, but the one I flagged as being the solution is simply more simpel (for me to read & understand) – frank Apr 15 '15 at 16:25

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.