I'd like to restart a program regardless of it already running or not using crontab.
I have this in crontab -e
* * * * * /usr/bin/pkill -f myapp; /home/ubuntu/xyz/bin/res.sh
And in the res.sh
#!/bin/bash
/usr/bin/pkill -f myapp
sleep 10
/home/ubuntu/xyz/bin/myapp & &>/dev/null
The problem is if the program is running, it kills the program. but it fails to start the program again.
I don't want to use any condition as to check if the program is already running and start only if it's not.
EDIT: The program is not a service to use "/etc/init.d/service restart" or "sudo service myapp restart"
Thanks.
Related
I'm trying to run the airodum-ng command for 5 seconds then stop it and save the output to the network variable, so I can print the output to a whiptail gui. Unfortunately the command just never stops running does anyone know how to fix this. I tried the timeout method witch you can see here and i also tried to use sleep but no luck.
function aircrack() {
sudo airmon-ng check kill
name=($(sudo /usr/sbin/airmon-ng ))
sudo airmon-ng start ${name[5]}
name=($(sudo /usr/sbin/airmon-ng ))
**netwroks=($(timeout 5s sudo airodump-ng ${name[5]}))**
}
In your code, the non-root timeout tries to kill a root process and fails.
The correct way to use timeout with sudo would be:
sudo timeout 5s airodump-ng ${name[5]}
I'm running Windows 10 x64 with WSL2 (Ubuntu 20.04 on WSL2).
In WSL2, I have cron running the following task:
* * * * * /mnt/c/Users/Colin/Desktop/test.sh
The contents of test.sh (currently executing every minute for test purposes) are as follows:
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32
taskkill.exe /im calibre.exe
sleep 5
<rsync command here>
echo "Done with rsync, launching calibre..."
screen -dm bash -c \"/mnt/c/Users/Colin/Desktop/startcalibre.sh\"
This script works perfectly when executed from the WSL2 prompt.
However, cron will not run either the taskkill.exe command nor the screen command (which launches calibre) in the script. I do see that cron executes the script because I see that rsync runs since I can see that in Wireshark. It seems that cron on WSL2 has problems with running Windows executables (taskkill.exe, etc) in particular (I can't even seem to get notepad.exe to launch via a cron-executed script).
What should I add to the script to get cron to execute Windows executables?
Not necessarily an answer, but I'm hoping this will lead us to one ...
First, let's try a script that pretty much anyone with WSL can run to see if we can reproduce the problem in more of an MRE:
#!/usr/bin/env bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32
echo $PATH > ~/cronlogger
notepad.exe &
sleep 2
which taskkill.exe >> ~/cronlogger
taskkill.exe /im notepad.exe >> ~/cronlogger 2>&1
I placed this script in my %userprofile%/Desktop/test.sh as well and set it to run with the same crontab line (via sudo crontab -u ntd -e):
* * * * * /mnt/c/Users/ntd/Desktop/test.sh
It works for me as expected -- Every minute, Notepad pops up and then after 2 seconds is killed. The contents of ~/cronlogger are:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32
/mnt/c/Windows/system32/taskkill.exe
SUCCESS: Sent termination signal to the process "Notepad.exe" with PID 4268.
Of course, wait until a few seconds after the top of the minute to make sure you capture the whole thing.
I notice that you have the ..\System32 path twice, but that shouldn't matter. Also, case-sensitivity isn't an issue unless you have specifically turned it on for Windows drives via /etc/wsl.conf.
I tested this:
On Windows 10 21H2
On Windows 11 using the latest Preview version of WSL
As my normal user sudo crontab -u ntd -e
As the root user sudo crontab -e
All worked okay for me.
At this point, the only thing I can think that might throw it off is if you were using a Systemd script of some sort. I've seen Systemd throw off the WSL binfmt_misc registration so that WSL can no longer run Windows executables. I could imagine this could happen inside the Cron daemon (but still be working via the default shell).
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
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;)
I have a glassfish install and I would like to have a simple terminal command checking if it's running. If it is, it will shutdown the process. Or if it not running, it will start the server.
To start the server I can enter:
/Applications/NetBeans/glassfish-3.1.1/bin/./asadmin start-domain domain
To stop the server I can enter:
/Applications/NetBeans/glassfish-3.1.1/bin/.asadmin stop-domain domain
I will would like to make a simple Alfred.app script than can start this domain if it's not running, or stop it if it's running.
One way to do this is to note in a file the process ID of the server when it starts, and have another script check to see if that process is running.
For example, script A (to run the server unconditionally):
#!/bin/sh
/Applications/NetBeans/glassfish-3.1.1/bin/./asadmin start-domain domain
# file name is arbitrary
pgrep whateverTheProcessNameIs > ~/.glassfish-server.pid
And in script B:
#!/bin/sh
pid=`pgrep -F ~/.glassfish-server.pid` # file chosen in script A
if [ "x$pid" = "x" ] ; then
# process has died; restart by running script A
/path/to/scriptA
fi
Note that only Mac OS X 10.8 (Mountain Lion) installs pgrep by default; otherwise you'd have to use some other method (like parsing ps output) to see what processes are running.
As far as how to run this check periodically, there are various ways. I'm assuming Alfred will run any script that is executable (chmod +x scriptA scriptB) but I don't know for sure.