root:/www> apachectl -k start
httpd: illegal option -- k
$ cat ./29176311.sh
#!/bin/bash
export PS4='$0 $LINENO: '
LOGNAME=$(id -nu)
if [ "Linux" = "$(uname -s)" ]
then
if [ "root" = "$LOGNAME" ]
then
# Version 0001.0001
# If apachectl not installed, install it => yum -y install httpd
# /usr/sbin/apachectl is a shell script executable file
# This file assigning the variable HTTPD='/usr/sbin/httpd'
# $ /usr/bin/file /usr/sbin/apachectl
# /usr/sbin/apachectl: Bourne shell script text executable
# /usr/sbin/httpd is an executable
# $ /usr/bin/file /usr/sbin/httpd
# /usr/sbin/httpd: ELF 64-bit LSB shared object, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.6.9, stripped
if [ ! -f /usr/sbin/apachectl ]
then
/usr/bin/yum -y install httpd
Ret=$?
else
Ret=0
fi
# If installed or /usr/sbin/apachectl is available, execute following statements
if [ 0 -eq $Ret ]
then
# Before starting httpd
# Check if a line starting with "^ServerName IPAddress" at /etc/httpd/conf/httpd.conf
grep -E "^ServerName" /etc/httpd/conf/httpd.conf >/dev/null 2>&1
if [ 0 -ne $? ]
then
grep -E "^#ServerName" /etc/httpd/conf/httpd.conf >/dev/null 2>&1
if [ 0 -eq $? ]
then
# If a line starting with #ServerName followed by 1 or more characters => #ServerName.*
# Replace #ServerName.* => \1 => first occurence of that line
# With:
# \1 new line
# ServerName IPAddress:80
# in same file /etc/httpd/conf/httpd.conf
# You can change port number to required port 420 :)
# Before assigning that port number
# Validate that port number not in use before assigning that number.
sed -i "s/\(^#ServerName.*\)/\1\nServerName $(hostname -i):80/;" /etc/httpd/conf/httpd.conf | grep ServerName
else
# If not present write following line at /etc/httpd/conf/httpd.conf
echo "ServerName $(hostname -i)" >> /etc/httpd/conf/httpd.conf
# Because of this change following error will not happen when we start httpd
# httpd: Could not reliably determine the server's fully qualified domain name, using 123.456.78.910 for ServerName
fi
fi
# Before strarting httpd validate if it is already at execution mode
ps -eaf |\
grep httpd |\
grep -E -v "grep|ps"
if [ 0 -ne $? ]
then
echo /usr/sbin/apachectl start
/usr/sbin/apachectl start
if [ 0 -eq $? ]
then
netstat -na | grep ":80"
echo "httpd start PASS"
else
echo "httpd start FAIL"
fi
else
echo -n "you can remove # at following lines to stop httpd." >/dev/null
#echo "Stopping httpd"
#echo /usr/sbin/apachectl stop
#/usr/sbin/apachectl stop
fi
else
echo "Execute this script using root user or using sudo"
fi
else
echo "Update this script to execute at other OS: $(uname -s | sed "s/\-[0-9]\.[0-9]$//;")"
fi
fi
murugesandins@123.456.78.910 /home/murugesandins [ 0 ]
$ ./29176311.sh
Execute this script using root user or using sudo
murugesandins@123.456.78.910 /home/murugesandins [ 0 ]
$ sudo ./29176311.sh
/usr/sbin/apachectl start
tcp 0 0 :::80 :::* LISTEN
httpd start PASS
murugesandins@123.456.78.910 /home/murugesandins [ 0 ]
$ sudo ./29176311.sh
root 6663 1 0 08:40 ? 00:00:00 /usr/sbin/httpd -k start
apache 6664 6663 0 08:40 ? 00:00:00 /usr/sbin/httpd -k start
apache 6665 6663 0 08:40 ? 00:00:00 /usr/sbin/httpd -k start
apache 6666 6663 0 08:40 ? 00:00:00 /usr/sbin/httpd -k start
apache 6667 6663 0 08:40 ? 00:00:00 /usr/sbin/httpd -k start
apache 6668 6663 0 08:40 ? 00:00:00 /usr/sbin/httpd -k start
apache 6669 6663 0 08:40 ? 00:00:00 /usr/sbin/httpd -k start
apache 6670 6663 0 08:40 ? 00:00:00 /usr/sbin/httpd -k start
apache 6671 6663 0 08:40 ? 00:00:00 /usr/sbin/httpd -k start
$ ./29176311.sh
Update this script to execute at other OS: CYGWIN_NT
$ file /cygdrive/c/Windows/System32/notepad.exe
/cygdrive/c/Windows/System32/notepad.exe: PE32+ executable (GUI) x86-64, for MS Windows
$ file /bin/ls
/bin/ls: PE32+ executable (console) x86-64, for MS Windows
$ /usr/sbin/apachectl
Usage: /usr/sbin/httpd [-D name] [-d directory] [-f file]
[-C "directive"] [-c "directive"]
[-k start|restart|graceful|graceful-stop|stop]
[-v] [-V] [-h] [-l] [-L] [-t] [-S]
Options:
-D name : define a name for use in <IfDefine name> directives
-d directory : specify an alternate initial ServerRoot
-f file : specify an alternate ServerConfigFile
-C "directive" : process directive before reading config files
-c "directive" : process directive after reading config files
-e level : show startup errors of level (see LogLevel)
-E file : log startup errors to file
-v : show version number
-V : show compile settings
-h : list available command line options (this page)
-l : list compiled in modules
-L : list available configuration directives
-t -D DUMP_VHOSTS : show parsed settings (currently only vhost settings)
-S : a synonym for -t -D DUMP_VHOSTS
-t -D DUMP_MODULES : show all loaded modules
-M : a synonym for -t -D DUMP_MODULES
-t : run syntax check for config files
$ man apachectl
$ man httpd
In general Apache should always be started and stopped using apachectl, not by executing httpd directly. The sole exception to that is at startup time, when systemd or SysVInit start it using their obscure startup files.