Cron job script termination - cron

hey guys I am writing a cron job where I am executing a script like this,
0 8 * * * /home/User/ABC/all_messages.sh
So usually when I run script manually I do ctrl + C to stop the script, how do I do this in cron job? Thanks

One way to stop this manually is following:
Find the PID of the process.
ps aux | grep /home/User/ABC/all_messages.sh
You will get an output like the following:
agamaga+ 40719 0.0 0.0 23756 940 pts/22 S+ 15:39 0:00 /home/User/ABC/all_messages.sh
Here the second column is the PID, i.e. 40719 for this example.
Kill the process using the following.
kill -9 <PID>
i.e.
kill -9 40719
This should terminate the process in question.
Another way is to terminate the process using its name (Although I don't prefer this one).
pkill /home/User/ABC/all_messages.sh

Related

"No such process" when trying to kill a running python script

I saw and tried many solutions.
I used ps aux | grep script.py to get the pid of the process. I got the following output: bioseq 24739 0.0 0.0 112884 1200 pts/1 R+ 13:20 0:00 grep --color=auto /script.py
, and then typed: kill 112884 and got the output 112884: No such process.
I also tried a similar command with grep -i, which yielded a different pid. kill <pid> also yielded <pid> No such process.
Try a pkill to kill the process, but you might also check your cron: it's possible that you kill the process but that the crontab restarts it constantly.
First of all, check whether The listed process was probably a
zombie process? therefore you cannot kill. Its live-time is depending on its parent process.
If you add the u flag to the call of ps, it displays also the STAT column which is Z for zombie processes.
if its a zombie process this has perfect explanation
How to kill zombie process
if its not a zombie process try this, killall [process name] command.
expects a process name, e.g. killall gedit which kills all such processes.
For more refer man killall

How to identify crontab child job?

My unix production server has test.ksh files, but every day it's running on daily basics using job.
I want to know which crontab job is calling this script. I checked usign below command, but i didn't find exact job name,
crontab -l
--It has been listed 100 job --
I have analysed above mentioned 100 job, but i didn't get test.ksh file
crontab -l | grep "test.ksh"
--file not found
But the file available in one directory, I can't find which job is called test.ksh script.
Finding:
1. Whether it's child job? - If yes, how can i identify the child job?
you could use pstree -p xxxx where xxxx is the pid of crond. You will then get a nice hierarchical overview of all offspring processes of crond.
If it is a child script, use ps -ef and use the ppid of the test.ksh job to identify the calling script.
For example, consider these two scripts, the first just calls the second
parent
#! /bin/sh
# Run child process
./child
child
#! /bin/sh
sleep 60
ps -ef shows (with a lot of other processes removed)
UID PID PPID C STIME TTY TIME CMD
501 5725 5724 0 8:22pm ttys000 0:00.28 -bash
501 6046 5725 0 11:38am ttys000 0:00.01 /bin/sh ./parent
501 6047 6046 0 11:38am ttys000 0:00.00 /bin/sh ./child
501 6048 6047 0 11:38am ttys000 0:00.00 sleep 60
The pid is the process identifier, so child has process id 6047. Its ppid - 6046 - is the process id of its parent as you can see looking at the entry for the parent process.

Two processes created by child_process.exec

I'm writing a pretty simple music server for my Raspberry Pi that will play MP#s on-demand. I can successfully launch an instance of mpg123 using child_process.exec(), but the PID of this new process differs from the process actually decoding the music. This is important to me because I want to track the process in the event of a user wanting to stop the job (child.kill()). I'm executing as follows:
var child = child_process.exec('mpg123 "' + filename + '"');
console.log(JSON.stringify({pid: child.pid});
This outputs {"pid":9444}, and music starts playing. However, when I check ps aux | grep mpg123, I see two different processes:
pi 9444 0.0 0.1 1760 504 pts/1 S+ 18:55 0:00 /bin/sh -c mpg123 "/home/pi/test.mp3"
pi 9445 11.0 0.6 14840 3112 pts/1 S+ 18:55 0:00 mpg123 /home/pi/test.mp3
This latter process, 9445, is what's actually playing my mp3. If I hang onto child and .kill() upon request, the first job is killed but the second lingers, making the kill ineffective.
Is there a different way to create a process that avoids this, or some better way to kill it? I'd rather not have to track everything (eg, the command I used to create it) and try to track down the 'correct' one based on that (eg, ps aux | grep <filename>).
According to this page, child_process.execFile(cmd, [args]) will spawn a new subshell, which is the pid of the .exec'd process. The subshell then created my instance of mpg123, which is the process I wanted to kill. Instead, I use:
var child = child_process.execFile('mpg123', [filename]); // note filename isn't enquoted
And when I want to kill it, I can simply:
child.kill(); // works

In unix I used kill command by providing a ppid then it close the terminal . why? kill -9 ppid

sleep 5000
In one terminal and in second terminal I'm running:
ps -ef | grep sleep
Then I'm killing this process in second terminal by using the ppid. Then it will close the first terminal where I run the sleep command. It will not create sleep command as an orphan.
$ ps -ef | grep sleep
trainee 4887 4864 0 17:05 pts/0 00:00:00 sleep 5000
trainee 4889 4264 0 17:05 pts/1 00:00:00 grep --color=auto sleep
kill -9 4864
Why?
Presumably the parent of the sleep is your shell. When you kill that your login is terminated and your terminal closes.
The Wikipedia article on Orphan process reads (in part),
An orphan process is a computer process whose parent process has finished or terminated, though it remains running itself.
and
A process can be orphaned unintentionally, such as when the parent process terminates or crashes. The process group mechanism in most Unix-like operation systems can be used to help protect against accidental orphaning, where in coordination with the user's shell will try to terminate all the child processes with the SIGHUP process signal, rather than letting them continue to run as orphans.

Running a php script in the background via shell - script never executes on mac os x

I have a php script which is responsible for sending emails based on a queue contained in a database.
The script works when it is executed from my shell as such:
/usr/bin/php -f /folder/email.php
However, when I execute it to run in the background:
/usr/bin/php -f /folder/email.php > /dev/null &
It never completes, and the process just sits in the process queue:
clickonce: ps T
PID TT STAT TIME COMMAND
1246 s000 Ss 0:00.03 login -pf
1247 s000 S 0:00.03 -bash
1587 s000 T 0:00.05 /usr/bin/php -f /folder/email.php
1589 s000 R+ 0:00.00 ps T
So my question is how can I run this as a background process and have it actually execute? Do I need to configure my OS? Do I need to change the way I execute the command?
"T" in the "STAT" column indicates a stopped process. I would guess that your script is attempting to read input from stdin and is getting stopped because it is not the foreground process and thus is not allowed to read.
You should check if the script does indeed read something while executing.

Resources