Bash code to check for SSH/Samba connections? - bash

I currently use the following cron job to put the computer in suspend mode and wake it up the following day:
0 13 * * * /usr/sbin/rtcwake -m standby -u -t $(date +\%s -d 'tomorrow 1145')
I'd like to replace this with a call to a Bash script that will
be called every hour…
check if anyone is currently connected through SSH or Samba (backup job)
If no one is connected, go into suspend mode
else try again the next hour.
Does someone have some working code I could use?
Thank you.

w:
Show who is logged on and what they are doing.

Related

xset in BASH script does not work under Cron

To learn about BASH scripting, I set myself the objective to write a Cron script which shuts down a PC with Mint 20 when activity on the Ethernet interface dropped below a threshold over an 1 hour.
I mainly (but not exclusively) use the PC as File/DLNA Server. The script works, but now I find that it also shuts down the PC the rare times I'm using the the front end. So I want my script to verify if the screen has been blanked (As per Power Management settings)
To test the principle I included this in my script:
screenon=$(/usr/bin/xset -q | grep 'Monitor is' | cut -d "s" -f 2)}
which when run in a terminal window gives (debug: set -x)
screenon= On
but when run from cron gives. (logger)
/usr/bin/xset: unable to open display ""
I have learned about similar problems, but cannot figure out how to solve this.
My script includes:PATH=$PATH:/usr/local/bin
and my PATH is: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
Thanks in advance for any help.

Ping google.com to see if vpn-server is up, if down, reconnect to another one

I am using a VPN server on a vps. The VPN service I use can of course go down for a few minutes or maybe even hours by accident or whatever. If it happens, I would like to quickly reconnect to another vpn-server with the command "vpnapp reconnect".
Does anyone know how to do a bash script I will run every minute via cronjob that:
Pings google.com
2A) If it can be reached, do nothing
2B) If it cannot be reached - which would mean the vpn server is down/not working - enter terminal command "vpnapp reconnect" which will make the vpn application reconnect to next available server they offer.
Would be so thankful if anyone could help me. I have ZERO programming knowledge. To me this is pure science.
Thanks in advance.
Oski
Can it be this simple?
if ping -c 4 google.com ; then exit ; else vpnapp reconnect ; fi
So if 4 packages are received, meaning google.com can be reached it will just exit the bash script.
if not, then it will just enter
vpnapp reconnect
as a command in the terminal... reconnecting the vpn.
or does it need to be "vpnapp reconnect" embedded in "?
Lol i am so weak at this. I really dont trust my own suggestion anyway so would love to hear a real programmer speak. Thanks!!
You don't need a bash script, you can just add this cron job (with crontab -e):
* * * * * ping -c2 google.com || vpnapp reconnect
* * * * * Run the command every minute.
ping -c2 google.com Send 2 pings to google.com.
|| vpnapp reconnect mean that if ping failled it will run vpnapp reconnect

script to start service from status check

I have an app on Centos that daily shuts down. Until I can get the support on it, I would like to handle it from a script in order when this occur again. My current script looks like this:
#!/bin/bash
if /path/to/service_check status service | grep -q 'SHUTDOWN'; then
/path/to/service_check start service
fi
It is setup in crontab to run on one minute interval but it is not running correctly. Is this approach incorrect?
Thank you!
check crond services is running systemctl status crond
check your script`s privilage chmod +x scriptname.sh
execute the script manually on the terminal /path/to/your/scriptname.sh
check your crontab configure,you should set the user to run this script * * * * * root /path/to/your/scriptname.sh

Trouble Automating Linux (Specifically Raspbian) Scripts

I've been experimenting with Pis for a little while now and am close to completing my first project, I have all the bits working but I'm struggling to put them all together into an automated process.
Basically I have a Pi setup to run a fbi slideshow from a specific folder and I want it to constantly be looking for a pre established Wifi network and when it finds this network it needs to run an update script. I've got these two bits working.
From here, I want the Pi to be turned on and load straight into fbi whilst running the checking script in the background, if the checking script finds the Wifi network then it will run as normal (preferably without ending the slideshow) and when it's done fbi should have an updated selection of images to run (if a restart of fbi is necessary then so be it).
I'm coming up short on achieving this. I can run one script or the other, and if I automate the checking script (cron hasn't worked though I dare say I'm doing something wrong) with rc.local it just gets stuck in a checking loop before login, which kinda makes sense given the script.
Here's the monitoring script:
#!/bin/bash
while true ; do
if ifconfig wlan0 | grep -q "inet addr:" ; then
echo "Wifi connected!"
echo "Initiating Grive sync!"
(cd /home/pi/images/; ./grive -s Pi_Test -V)
sleep 60
else
echo "Wifi disconnected! Attempting to reconnect now."
ifup --force wlan0
sleep 10
fi
done
and in case it's relevant, here's the command the run the fbi slideshow:
fbi -noverbose -a -t 10 -u /home/pi/images/Pi_Test/*.jpg
I do not have a Pi but I have used cron on my VPS which is running a CentOS, but the overall procedure should be similar.
To have some script to be executed by cron, you need to:
Edit /etc/cron.allow
You need to add in your user id to this file so that you can use crontab.
crontab -e
Use this command to add rules you want to fire.
From your description, it seems to me that you already know the syntax to add rules into cron table.
After that, you can use crontab -l to verify your change.
As for stuck at before login, that is very likely due to the while loop. You might want to get rid of the while and sleep because cron is helping you out by invoking your script periodically.
Therefore the following should not suffer from the stuck issue.
if ifconfig wlan0 | grep -q "inet addr:" ; then
echo "Wifi connected!"
echo "Initiating Grive sync!"
(cd /home/pi/images/; ./grive -s Pi_Test -V)
else
echo "Wifi disconnected! Attempting to reconnect now."
ifup --force wlan0
fi
The trick instead, is to have some line similar to this in your cron table
*/1 * * * * /home/David_Legassick/test.sh
The */1 asks cron to call your script test.sh every minute.

use ssh to kill and restart all sshd processes

I have a script that will
kill all sshd processes
start a new sshd process
I would like to scp this script onto a remote computer and execute it using ssh. After it executes the first step of killing all sshd, will it still get to the 2nd step of running sshd again? I'm worried because I'm running the script using ssh and ssh will die after step 1.
Normal procedure is to stop the main sshd, with e.g. /etc/init.d/sshd stop or your distro's equivalent. This way, the listening daemon shuts down while existing connections go on until the clients disconnect.
If you want to upgrade/replace sshd, change any settings and restart it, this is the way to go.
No need to scp it to server, just try doing this :
while read cmd; do ssh server "bash -c $cmd"; done < script.sh
Why not to use cron for it?
For example:
10 * * * * /path_to_script
minute hour day month dayofweek command
Do not forget to switch it off;)

Resources