I am writing a script that is runs a program using nohup that contains parameters, thus I wrap the command into a shell command.
I am trying to get the pid of the program but am running into difficulties.
This is what I am trying to run:
nohup bash -c '../command --dir /mnt/raid/test/test.txt "https://www.google.com/" > temp.txt 2>&1 &'; APP_PID=$! | {
#APP_PID=$!
echo "the pid is ${APP_PID}"
}
The result of this script shows an empty pid as below:
the pid is
I have tried numerous variations and have searched Stack Overflow but could not find anything related.
Question is what am I doing wrong?
The background process is being created as a grand child of your shell (by bash -c) and you won't be able to get the PID of the grand child from the outer shell. You could do this instead:
nohup bash -c '../command --dir /mnt/raid/test/test.txt "https://www.google.com/" > temp.txt 2>&1'& APP_PID=$! && {
echo "the pid is ${APP_PID}"
}
Here, nohup runs in background and you get its PID.
Related
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 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
What would be the correct format for the following, where I want to execute two scripts? The following is only executing the first one for me:
if ps aux | grep -E "[a]ffiliate_download.py|[g]oogle_download.py" > /dev/null
then
echo "Script is already running. Skipping"
else
exec "$DIR/affiliate_download.py"
exec "$DIR/google_download.py"
fi
The exec command replaces the current shell process with the program it runs. Since the shell is no longer running, it can't run commands after that.
Just execute the commands normally:
else
"$DIR/affiliate_download.py"
"$DIR/google_download.py"
fi
I have a bash script which I want it to run as a daemon as it checks for a condition does some work and sleeps for some time and the cycle repeats.
I demonize the script as : nohup myScript.sh &
How can I setup the logging for the same script as it has same echo commands which prints some statements and how to make sure I have the pid of the script if I want to kill it later.
Can anyone suggest how I can achieve the same
If you look at the I/O redirection chapter in the Advanced Bash-Scripting Guide, then you see that you can redirect all output to a file by doing something like, in the top of your script:
exec 2>&1
exec 1>>out.log
The first line redirects stderr to stdout, and the second line appends stdout to out.log. If you do this, then you can use echo to write specific messages to the log-file, and the output of all commands, which you haven't silenced will be written to the log-file as well.
Bash has a lot of special variables, you can get an overview of these in the Special Shell Variables reference card. One of these is $$, which hold the scripts PID. To save this, you can do the following:
echo $$ > ${0}.pid
This will create a file name <file name of script>.pid, containing the PID of the script.
Put the following line in the beginning of your script. It saves the pid for later :
echo $$ > myScript.pid
Start your script capturing output for logging (from the nohup man page example) :
nohup myScript.sh > myScript.log &
Kill the script later by :
kill -9 `cat myScript.pid`
Simple myScript.sh example :
#!/bin/bash
echo $$ > myScript.pid
while :; do echo "Hello"; sleep 1; done
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
$