Check if a nohup script is running or start it - bash

we are running a tool on different machine by calling the shell script to run it,
the script is in background by using "nohup scriptname ", for some reasons which i don't know the script stops after some time, i want to make a script to keep on checking if the script is stopped and runs it again.
I have very little knowledge on shell script, but suddenly this requirement came and i searched on google but not getting proper answer, please help.

Here is a simple and working solution.
First, the script which has to be run again, let's call it bar.sh:
#! /bin/bash
echo "bar is living"
sleep 5
echo "bar is dying"
exit
Second, the script which runs bar.sh and watches for his death, called foo.sh:
#! /bin/bash
while true ; do
echo "Running bar ..."
./bar.sh &
echo "Waiting bar's termination"
wait
done
Now just type in the terminal:
$ chmod +x foo.sh bar.sh
$ ./foo.sh

Related

Using [ nohup ksh ] to run a script cannot see the sleep process

I am using CentOS 7.6 and bash shell inside VirtualBox .
I came across a script named MotherScript in my site having this statement :
nohup ksh MyScript &
Both MotherScript and MyScript have a shebang line
#!/bin/ksh
After login, I just fire the MotherScript.
In my previous companies, usually, I used this statement :
nohup MyScript &
Is there any functional/strange/usage difference between these 2 statements ?
Inside MyScript, there is a sleep command to sleep 1 hour repeatedly in a while loop for 7 days.
However, very strange, I cannot see this sleep process using :
ps -ef | grep sleep
Therefore, I cannot kill the sleep process.
Does this nohup ksh play the trick ?
Anyone know the reason behind ?
Thanks.
Alvin
nohup ksh MyScript &
This will run the script using ksh(korn) shell.
nohup MyScript &
This will pick your default shell which you can check using below command
echo $SHELL
you can also change default shell using chsh Utility

Run two xterm commands at the same time in BASH [duplicate]

This question already has answers here:
How do you run multiple programs in parallel from a bash script?
(19 answers)
Closed 4 years ago.
I am trying to run two different programs in xterm windows from the same automation script. My current code looks like this:
#!/bin/bash/sh
echo "STARTING PROGRAM ONE"
# change into correct directory
cd ~/myProjects/ProgramOne
xterm -e myProg1 -a P1 &> /tmp/ProgramOne/P1.txt
echo "STARTING PROGRAM TWO"
# change into correct directory
cd ~/myProjects/ProjectTwo
xterm -e myProg2 -a P2 &> /tmp/ProgramTwo/P2.txt
# Code to kill the xterm process?
echo "******************************************"
echo "START AUTOMATION COMPLETE"
echo "******************************************"
What I am looking to accomplish is to have two separate programs, in different directories, run in two different xterm windows so I can demonstrate to the end user that the programs are running appropriately.
Currently, the first program executes fine, and when I Ctrl + C it, the second kicks off just fine. However, I would like both to execute at the same time.
I have looked at a few resources on SO but have not found anything to help me with this problem.
I am on a CentOS7 system, trying to automate this process. Any help or advice would be great.
Thanks!
Start them in the background and wait for them to finish:
#!/bin/bash/sh
echo "STARTING PROGRAM ONE"
# change into correct directory
cd ~/myProjects/ProgramOne
xterm -e myProg1 -a P1 &> /tmp/ProgramOne/P1.txt &
echo "STARTING PROGRAM TWO"
# change into correct directory
cd ~/myProjects/ProjectTwo
xterm -e myProg2 -a P2 &> /tmp/ProgramTwo/P2.txt &
# Code to kill the xterm process?
wait
echo "******************************************"
echo "START AUTOMATION COMPLETE"
echo "******************************************"

Run bash script in background by default

I know I can run my bash script in the background by using bash script.sh & disown or alternatively, by using nohup. However, I want to run my script in the background by default, so when I run bash script.sh or after making it executable, by running ./script.sh it should run in the background by default. How can I achieve this?
Self-contained solution:
#!/bin/sh
# Re-spawn as a background process, if we haven't already.
if [[ "$1" != "-n" ]]; then
nohup "$0" -n &
exit $?
fi
# Rest of the script follows. This is just an example.
for i in {0..10}; do
sleep 2
echo $i
done
The if statement checks if the -n flag has been passed. If not, it calls itself with nohup (to disassociate the calling terminal so closing it doesn't close the script) and & (to put the process in the background and return to the prompt). The parent then exits to leave the background version to run. The background version is explicitly called with the -n flag, so wont cause an infinite loop (which is hell to debug!).
The for loop is just an example. Use tail -f nohup.out to see the script's progress.
Note that I pieced this answer together with this and this but neither were succinct or complete enough to be a duplicate.
Simply write a wrapper that calls your actual script with nohup actualScript.sh &.
Wrapper script wrapper.sh
#! /bin/bash
nohup ./actualScript.sh &
Actual script in actualScript.sh
#! /bin/bash
for i in {0..10}
do
sleep 10 #script is running, test with ps -eaf|grep actualScript
echo $i
done
tail -f 10 nohup.out
0
1
2
3
4
...
Adding to Heath Raftery's answer, what worked for me is a variation of what he suggested such as this:
if [[ "$1" != "-n" ]]; then
$0 -n & disown
exit $?
fi

AppleScript won't run shell script as is in Terminal

I have a .sh script that works fine if I run it in Terminal using "/Volumes/MEDIA/SERVER/SYNC.sh"
But I can not get it to run the same in AppleScript Editor using:
do shell script "/Volumes/MEDIA/SERVER/SYNC.sh"
Also tried the above with bash in front, sh in front.
The shell script (SYNC.sh)
#!/bin/bash
login="uhh"
pass="uhh"
host="uhh.com"
remote_dir='~/private/sync'
local_dir="/Volumes/MEDIA/_/SYNCING"
base_name="$(basename "$0")"
lock_file="/tmp/$base_name.lock"
trap "rm -f $lock_file" SIGINT SIGTERM
if [ -e "$lock_file" ]
then
echo "$base_name is running already."
exit
else
touch "$lock_file"
lftp -p 22 -u "$login","$pass" sftp://"$host" << EOF
set sftp:auto-confirm yes
set mirror:use-pget-n 5
mirror -c -P5 --Remove-source-files --log="/Volumes/MEDIA/SERVER/LOGS/$base_name.log" "$remote_dir" "$local_dir"
quit
EOF
# MOVE FINISHED FILES INTO DIRECTORY FOR CONVERSION
mv /Volumes/MEDIA/_/SYNCING/movies/* /Volumes/MEDIA/SEEDBOX/MOVIES
mv /Volumes/MEDIA/_/SYNCING/tvshows/* /Volumes/MEDIA/SEEDBOX/TVSHOWS
mv /Volumes/MEDIA/_/SYNCING/books/* /Volumes/MEDIA/SEEDBOX/BOOKS
mv /Volumes/MEDIA/_/SYNCING/music/* /Volumes/MEDIA/SEEDBOX/MOVIES
# SHOW COMPLETED NOTIFICIATION
osascript -e 'display notification "Sync completed" with title "SEEDB0X"'
rm -f "$lock_file"
trap - SIGINT SIGTERM
exit
fi
By not 'the same' what happens is only the
osascript -e 'display notification "Sync completed" with title "SEEDB0X"'
is run. With the script running through Terminal that only appears once syncing is done.
Thanks for any help!
Did you install lftp yourself? I don't have a Mac handy to check if it's in Mac OS X by default or not. If you installed it, then it probably isn't in the PATH of the AppleScript environment and the bash script can't find it when run from there.
If this is the case, then you'll have to either:
Fully qualify the path to 'lftp' (eg, "/usr/local/bin/lftp" or where ever it actually is)
or
Append to the PATH environment variable as used by AppleScript (or early in your bash script).
I think I'd go for option 1. Option 2 is overkill and more likely to adversely affect other things at other times.
PS. If you don't know where 'lftp' is installed, type 'which lftp' in the terminal.

shell script to start conky and plank

I have just started to learn shell scripting on Ubuntu and thought of writing a basic script which starts conky and plank and placing it in /usr/bin so that I can run it as a command.I did make it an executable too.
#!/bin/bash
echo `conky -q &`
echo `plank &`
Only conky gets started up.
You don't need the echoes or the back quotes. Just:
#!/bin/bash
conky -q &
plank &
This should also work:
#!/bin/bash
echo `conky -q` &
echo `plank` &
So you put in background the whole command.

Resources