how to run shell script in background in unix - shell

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

Related

How to run two separate bash commands at the same time, not in the same line but together Parallelly [duplicate]

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

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

Execute a script through ssh and store its pid in a file on the remote machine [duplicate]

This question already has answers here:
How to pass argument with exclamation mark on Linux?
(3 answers)
Closed 3 years ago.
I am not able to store any PID in a file on the remote machine when running a script in background through ssh.
I need to store the PID of the script process in a file in purpose to kill it whenever needed. When running the exact command on the remote machine it is working, why through ssh it is not working so ?
What is wrong with the following command:
ssh user#remote_machine "nohup ./script.sh > /dev/null 2>&1 & echo $! > ./pid.log"
Result: The file pid.log is created but empty.
Expected: The file pid.log should contain the PID of the running script.
Use
ssh user#remote_machine 'nohup ./script.sh > /dev/null 2>&1 & echo $! > ./pid.log'
OR
ssh user#remote_machine "nohup ./script.sh > /dev/null 2>&1 & echo \$! > ./pid.log"
Issue:
Your $! was getting expanded locally, before calling ssh at all.
Worse, before calling the ssh command, if there was a process stared in the background, then $! would have expanded to that and complete ssh command would have got expanded to contain that PID as argument to echo.
e.g.
$ ls &
[12342] <~~~~ This is the PID of ls
$ <~~~~ Prompt returns immediately because ls was stared in background.
myfile1 myfile2 <~~~~ Output of ls.
[1]+ Done ls
#### At this point, $! contains 12342
$ ssh user#remote "command & echo $! > pidfile"
# before even calling ssh, shell internally expands it to:
$ ssh user#remote "command & echo 12342 > pidfile"
And it will put the wrong PID in the pidfile.

nohup in shell script does not work in debug mode with set -x

The script test.sh
#!/bin/bash
set -x
nohup echo hello &
When run ./test.sh, output is
+ nohup echo hello
appending output to nohup.out
But when ssh is setup and run ssh localhost ./test.sh, output is nothing.
Everything is fine.
The command is executed, but you won't see the output, by that time the ssh connection was already terminated.
Check nohup.out, it will show that the command was indeed executed.

How to include nohup inside a bash script?

I have a large script called mandacalc which I want to always run with the nohup command. If I call it from the command line as:
nohup mandacalc &
everything runs swiftly. But, if I try to include nohup inside my command, so I don't need to type it everytime I execute it, I get an error message.
So far I tried these options:
nohup (
command1
....
commandn
exit 0
)
and also:
nohup bash -c "
command1
....
commandn
exit 0
" # and also with single quotes.
So far I only get error messages complaining about the implementation of the nohup command, or about other quotes used inside the script.
cheers.
Try putting this at the beginning of your script:
#!/bin/bash
case "$1" in
-d|--daemon)
$0 < /dev/null &> /dev/null & disown
exit 0
;;
*)
;;
esac
# do stuff here
If you now start your script with --daemon as an argument, it will restart itself detached from your current shell.
You can still run your script "in the foreground" by starting it without this option.
Just put trap '' HUP on the beggining of your script.
Also if it creates child process someCommand& you will have to change them to nohup someCommand& to work properly... I have been researching this for a long time and only the combination of these two (the trap and nohup) works on my specific script where xterm closes too fast.
Create an alias of the same name in your bash (or preferred shell) startup file:
alias mandacalc="nohup mandacalc &"
Why don't you just make a script containing nohup ./original_script ?
There is a nice answer here: http://compgroups.net/comp.unix.shell/can-a-script-nohup-itself/498135
#!/bin/bash
### make sure that the script is called with `nohup nice ...`
if [ "$1" != "calling_myself" ]
then
# this script has *not* been called recursively by itself
datestamp=$(date +%F | tr -d -)
nohup_out=nohup-$datestamp.out
nohup nice "$0" "calling_myself" "$#" > $nohup_out &
sleep 1
tail -f $nohup_out
exit
else
# this script has been called recursively by itself
shift # remove the termination condition flag in $1
fi
### the rest of the script goes here
. . . . .
the best way to handle this is to use $()
nohup $( command1, command2 ...) &
nohup is expecting one command and in that way You're able to execute multiple commands with one nohup

Resources