Bash script to open firefox on successful ping - linux

As the title suggests I am trying to use bash to ping a server then when it gets a connection it will open Firefox to a page displaying an alarm screen. This will happen after a power down when the computer automatically boots then sometimes can take time to connect to the network. If I set Firefox to start on start-up then sometimes it displays the no connection screen.
Here is what I have already, but it does not seem to stop after a successful ping's.
#!/bin/bash
success=0
ping hercules
while [ $success -ne 1 ]; do
ping -c 4 hercules
if [ $? -eq 0 ]; then
success=0
/usr/bin/firefox
else
success=1
fi
done
I am sure I am doing something pretty stupid and this should no be that hard.

Just to fix your script, you can simply exit the script on success:
#!/bin/bash
while true ; do
if ping -c 4 hercules ; then
/usr/bin/firefox
exit 0
fi
done

If I understand your question correctly, what you want is to keep pinging. When it succeeds, to start firefox, and stop pinging? You are currently setting success=1 when it fails, and then exiting the while loop, as success -ne 1 is false.
#!/bin/bash
success=0
ping hercules
while [ $success -ne 1 ]; do
ping -c 4 hercules
if [ $? -eq 0 ]; then
success=1
/usr/bin/firefox
fi
done

#!/bin/bash
ping -t 1 8.8.8.8 #(-t on mac / else it -w)
SUCCESS=0
RETVAL=$?
while [ $SUCCESS -ne 1 ]; do
if [ $RETVAL -eq 0 ]; then
SUCCESS=1
'LAUNCH FIREFOX HERE IF THE PING WORKS'
else
'DO SOMETHING'
fi
done

Related

SSH Remote command exit code

I know there are lots of discussions about it but i need you help with ssh remote command exit codes. I have that code:
(scan is a script which scans for viruses in the given file)
for i in $FILES
do
RET_CODE=$(ssh $SSH_OPT $HOST "scan $i; echo $?")
if [ $? -eq 0 ]; then
SOME_CODE
The scan works and it returns either 0 or (1 for errors) or 2 if a virus is found. But somehow my return code is always 0. Even, if i scan a virus.
Here is set -x output:
++ ssh -i /home/USER/.ssh/id host 'scan Downloads/eicar.com; echo 0'
+ RET_CODE='File Downloads/eicar.com: VIRUS: Virus found.
code of the Eicar-Test-Signature virus
0'
Here is the Output if i run those commands on the "remote" machine without ssh:
[user#ws ~]$ scan eicar.com; echo $?
File eicar.com: VIRUS: Virus found.
code of the Eicar-Test-Signature virus
2
I just want to have the return Code, i dont need all the other output of scan.
!UPDATE!
It seems like, echo is the problem.
The reason your ssh is always returning 0 is because the final echo command is always succeeding! If you want to get the return code from scan, either remove the echo or assign it to a variable and use exit. On my system:
$ ssh host 'false'
$ echo $?
1
$ ssh host 'false; echo $?'
1
$ echo $?
0
$ ssh host 'false; ret=$?; echo $ret; exit $ret'
1
$ echo $?
1
ssh returns the exit status of the entire pipeline that it runs - in this case, that's the exit status of echo $?.
What you want to do is simply use the ssh result directly (since you say that you don't want any of the output):
for i in $FILES
do
if ssh $SSH_OPT $HOST "scan $i >/dev/lull 2>&1"
then
SOME_CODE
If you really feel you must print the return code, that you can do that without affecting the overall result by using an EXIT trap:
for i in $FILES
do
if ssh $SSH_OPT $HOST "trap 'echo \$?' EXIT; scan $i >/dev/lull 2>&1"
then
SOME_CODE
Demo:
$ ssh $host "trap 'echo \$?' EXIT; true"; echo $?
0
0
$ ssh $host "trap 'echo \$?' EXIT; false"; echo $?
1
1
BTW, I recommend you avoid uppercase variable names in your scripts - those are normally used for environment variables that change the behaviour of programs.

Bash exit status not working in script

I've written a script that starts, stops and sends status of Apache, with messages dependent on the output of the commands.
I have most of it correct, but my errors are not printing out correctly. In other words, even if I do not have Apache loaded, "stopping" it still shows a successful message.
I need help getting my error messages to print when necessary.
#!/bin/bash
echo -e "\e[1;30mApache Web Server Control Script\e[0m"
echo
echo "Enter the operation number to perform (1-4): "
echo " 1 - Start the httpd server"
echo " 2 - Restart the httpd server"
echo " 3 - Stop the httpd server"
echo " 4 - Check httpd server status"
echo
echo -n "===> "
read NUMBER
EXITSTATUS=$?
echo
if [ $NUMBER -eq "1" ]; then
systemctl start httpd
if [ $EXITSTATUS -eq "0" ]; then
echo -e "\e[1;32mThe return value of the command 'systemctl
start httpd' was 0.\e[0m"
echo -e "\e[1;32mThe Apache web server was successfully
started.\e[0m"
else
echo -e "\e[1;31mThe return value of the command 'systemctl
start httpd' was 5.\e[0m"
echo -e "\e[1;31mThe Apache web server was not successfully
started.\e[0m"
fi
fi
if [ $NUMBER -eq "2" ]; then
systemctl restart httpd
if [ $EXITSTATUS -eq "0" ]; then
echo -e "\e[1;32mThe return value of the command 'systemctl
restart httpd' was 0.\e[0m"
echo -e "\e[1;32mThe Apache web server was successfully
restarted.\e[0m"
else
echo -e "\e[1;31mThe return value of the command 'systemctl
restart httpd' was 5.\e[0m"
echo -e "\e[1;31mThe Apache web server was not successfully
restarted.\e[0m"
fi
fi
if [ $NUMBER -eq "3" ]; then
systemctl stop httpd
if [ $EXITSTATUS -eq "0" ]; then
echo -e "\e[1;32mThe return value of the command 'systemctl
stop httpd' was 0.\e[0m"
echo -e "\e[1;32mThe Apache web server was successfully
stopped\e[0m."
else
echo -e "\e[1;31mThe return value of the command 'systemctl
stop httpd' was 5.\e[0m"
echo -e "\e[0;31mThe Apache web server was successfully
stopped.\e[0m"
fi
fi
if [ $NUMBER -eq "4" ]; then
systemctl status httpd
if [ $EXITSTATUS -eq "0" ]; then
msg=$(systemctl status httpd)
else
echo -e "\e[1;31mThe Apache web server is not currently
running.\e[0m"
echo $(msg)
fi
fi
if [[ $NUMBER != [1-4] ]]; then
echo -e "\e[1;31mPlease select a valid choice: Exiting.\e[0m"
fi
exit 0
The variable EXITSTATUS doesn't contain the exit code of the systemctl calls, but that of the read command. You could rewrite it either as
systemctl start httpd
EXITSTATUS=$?
if [ $EXITSTATUS -eq 0 ]; then
[...]
or more simply as
systemctl start httpd
if [ $? -eq 0 ]; then
[...]
Storing the value of $? in a variable is only necessary if you either want to use it afterwards in another place (e. g. as exit code of your own script), or have to make other calls before branching on the value.
You're not setting your variable $EXITSTATUS after running the commands, so it maintains its original value (the exit status of read NUMBER).
Since you only care about whether the command succeeded or not, better would be to avoid using it entirely and change the conditions to e.g.:
if systemctl restart httpd; then
# it was successful ($? would be 0)
fi

Linux Script to check if process is running and restart if not

I am having this script which looks for the process filebeat and restarts it if is not running. Cron runs this script every 5 minutes. Most of the time this works fine except sometime it creates multiple filebeat process. Can someone please point out what is the issue in my script.
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
service=filebeat
servicex=/usr/share/filebeat/bin/filebeat
pid=`pgrep -x "filebeat"`
if [ $pid > /dev/null ]
then
echo "$(date) $service is running!!!"
else
echo "$(date) starting $service"
cd /home/hpov/beats/filebeat
./filebeat -c filebeat.yml &
fi
#!/bin/bash
pidof script.x86 >/dev/null
if [[ $? -ne 0 ]] ; then
echo "Restarting script: $(date)" >> /var/log/script.txt
/etc/script/script.x86 &
fi
Super easy :D

can shell script make itself run in background after running some steps?

I have BBB based custom Embedded Linux based board with busybox shell(ash)
I have a situation where my script must run in background with following condition
There must only one instance of the script.
wrapper script need to know if script started successfully in background or not.
There is another wrapper script which starts and stops my script, wrapper script is as mentioned below.
#!/bin/sh
export PATH=/bin:/sbin:/usr/bin:/usr/sbin
readonly TEST_SCRIPT_PATH="/home/testscript.sh"
readonly TEST_SCRIPT_LOCK_PATH="/var/run/${TEST_SCRIPT_PATH##*/}.lock"
start_test_script()
{
local pid_of_testscript=0
local status=0
#Run test script in background
"${TEST_SCRIPT_PATH}" &
#---------Now When this point is hit, lock file must be created.-----
if [ -f "${TEST_SCRIPT_LOCK_PATH}" ];then
pid_of_testscript=$(head -n1 ${TEST_SCRIPT_LOCK_PATH})
if [ -n "${pid_of_testscript}" ];then
kill -0 ${pid_of_testscript} &> /dev/null || status="${?}"
if [ ${status} -ne 0 ];then
echo "Error starting testscript"
else
echo "testscript start successfully"
fi
else
echo "Error starting testscript.sh"
fi
fi
}
stop_test_script()
{
local pid_of_testscript=0
local status=0
if [ -f "${TEST_SCRIPT_LOCK_PATH}" ];then
pid_of_testscript=$(head -n1 ${TEST_SCRIPT_LOCK_PATH})
if [ -n "${pid_of_testscript}" ];then
kill -0 ${pid_of_testscript} &> /dev/null || status="${?}"
if [ ${status} -ne 0 ];then
echo "testscript not running"
rm "${TEST_SCRIPT_LOCK_PATH}"
else
#send SIGTERM signal
kill -SIGTERM "${pid_of_testscript}"
fi
fi
fi
}
#Script starts from here.
case ${1} in
'start')
start_test_script
;;
'stop')
stop_test_script
;;
*)
echo "Usage: ${0} [start|stop]"
exit 1
;;
esac
Now actual script "testscript.sh" looks something like this,
#!/bin/sh
#Filename : testscript.sh
export PATH=/bin:/sbin:/usr/bin:/usr/sbin
set -eu
LOCK_FILE="/var/run/${0##*/}.lock"
FLOCK_CMD="/bin/flock"
FLOCK_ID=200
eval "exec ${FLOCK_ID}>>${LOCK_FILE}"
"${FLOCK_CMD}" -n "${FLOCK_ID}" || exit 0
echo "${$}" > "${LOCK_FILE}"
# >>>>>>>>>>-----Now run the code in background---<<<<<<
handle_sigterm()
{
# cleanup
"${FLOCK_CMD}" -u "${FLOCK_ID}"
if [ -f "${LOCK_FILE}" ];then
rm "${LOCK_FILE}"
fi
}
trap handle_sigterm SIGTERM
while true
do
echo "do something"
sleep 10
done
Now in above script you can see "---Now run the code in background--" at that point I am sure that either lock file is successfully created or instance of this script is already running. So Then I can safely run other code in background and wrapper script can check for lockfile and find out if the process mentioned in the lock file is running or not.
can shellscript itself make it to run in background ?
if not is there a better way to meet all the conditions ?
I think you can look into job control built-in, specifically bg.
Job Control Commands
When processes say they background themselves, what they actually do is fork and exit the parent. You can do the same by running whichever commands, functions or statements you want with & and then exiting.
#!/bin/sh
echo "This runs in the foreground"
sleep 3
while true
do
sleep 10
echo "doing background things"
done &

How to check in dash for root

The standard-solution for bash, see:
https://askubuntu.com/questions/15853/how-can-a-script-check-if-its-being-run-as-root
which is:
#!/bin/bash
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
does not work in dash, which is slowly becoming the standard-shell under Linux. How can the above be ported to dash?
Use id:
if [ "$(id -u)" -eq 0 ]; then
echo "I am root!"
fi
Or
#!/bin/sh
if [ "$(id -u)" -ne 0 ]; then
echo "This script must be run as root"
exit 1
fi
What I usually do is check for the capabilities I actually require, so that the script will also work correctly for a user who likes to run via an alternate privileged account (*BSD used to have toor for superuser with csh, which of course nobody in their right mind would want these days, but anyway).
test -w /usr/share/bin ||
{ echo "$0: /usr/share/bin not writable -- aborting" >&2; exit 1 }
Use the "id -u" command to get your current effective user id:
#!/bin/dash
MYUID=`id -u`
if [ "$MYUID" -eq 0 ]
then
echo "You are root"
else
echo "You are the non-root user with uid $MYUID"
fi

Resources