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
Related
how to run multiple bash scripts in 1 bash command ?
i use command
bash script1.sh
how to make it run multiple commands in 1 command ?
for example
bash script1.sh bash script2.sh bash script3.sh bash script4.sh
Please help
If you want to run all bash script in //:
for i in {1..4}; do bash "script${i}.sh" & done
If you put the control operator & at the end of a command, e.g. command &, the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0. Pid of the last backgrounded command is available via the special variable $!
If instead you want to run sequentially, use
printf '%s\n' script{1..4}.sh | xargs -n1 bash
or
for i in {1..4}; do bash "script${i}.sh"; done
how to run shell script in background in unix?
My script
#!/bin/sh
while [ true ]
do
ps -fu $USER>>/home/axway/trace.log 2>&1
sleep 10
done
running above script (shellEx1.sh) in background by nohup command on promt
nohup ./shellEX1.sh &
having below isuue:
$ nohup ./shellEX1.sh &
[3] 19520
$ nohup: ignoring input and appending output to `nohup.out'
Its warning to say like the output of the script will be written in file 'nohup.out'. In order to remove this warning, you can try
nohup ./shellEX1.sh >/tmp/output.txt &
or
nohup ./shellEX1.sh >/dev/null &
Just a thought, you could make it connect or create a screen instance at the start.
screen -S bashscript
my bash script
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
how to run shell script in background in unix?
My script
#!/bin/sh
while [ true ]
do
ps -fu $USER>>/home/axway/trace.log 2>&1
sleep 10
done
running above script (shellEx1.sh) in background by nohup command on promt
nohup ./shellEX1.sh &
having below isuue:
$ nohup ./shellEX1.sh &
[3] 19520
$ nohup: ignoring input and appending output to `nohup.out'
Its warning to say like the output of the script will be written in file 'nohup.out'. In order to remove this warning, you can try
nohup ./shellEX1.sh >/tmp/output.txt &
or
nohup ./shellEX1.sh >/dev/null &
Just a thought, you could make it connect or create a screen instance at the start.
screen -S bashscript
my bash script
I was learning bash with a book called learning the bash shell. All was well until I came across the kill command. I created a little script called loop, then ran it using loop &. When I use kill %loop, then use ps -e | grep loop, the process is still there. So did I fail to kill it? Do I have to use kill -9? Also,pidof seemed to not work at all when I use pidof loop. It returns nothing at all.
I am using Ubuntu and bash. Although I think the script shouldn't matter, here it is just in case:
#! /bin/bash
# Just a script
while true; do
sleep 60
done
Shell scripts don't run as the name of the script. They run as the name of the shell you specify in the shebang. In your case, that's /bin/bash. If you'd done pidof bash you'd have gotten the pid of your script (along with all the other bash scripts running at the time).
That being said, you can't kill scripts by their name, as you're trying with kill %loop. Kill only accepts pids. If you're using the % notation, then that refers to the internal job number assigned by the shell. e.g, using your loop script as an example:
$ ./loop & <-- first run of loop script
[1] 8077 <--- job #1, running as pid 8077
$ ./loop & <--- second run of loop script
[2] 8079 <-- job #2, running as pid 8079
$ jobs
[1]- Running ./loop &
[2]+ Running ./loop &
$ kill %2
$ jobs
[1]- Running ./loop &
[2]+ Terminated ./loop <-- note, showing 'terminated' now
$ kill -9 8077
$ jobs
[1]+ Killed ./loop
$ jobs
$