Amit
Amit

Reputation: 495

Two Shell Script instance while calling through cron job

I have one simple script which echoes value of for loop. I am calling same using a cron job and I ran grep command it shows two instances.

Script::

 #!/bin/bash
 for i in {1..999999}
 do
  echo "Welcome $i times"
 done

Cron Command::

* * * * * /home/amit/Desktop/crontest/test.sh > /home/amit/Desktop/crontest/null 2 >&1.

ps Grep command::

$ ps -ef | grep test

amit 5853  5852  0 23:28 ?  00:00:00 /bin/sh -c /home/amit/Desktop/crontest/test.sh > /home/amit/Desktop/crontest/null 2>&1
amit 5854  5853 99 23:28 ?  00:00:07 /bin/bash /home/amit/Desktop/crontest/test.sh 2

My question is::

It's really a two instance or it just a way how cron job run.

Upvotes: 0

Views: 258

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295413

Cron implicitly runs the line given with sh -c.

If that line starts another shell (without recognizing it as the only command to run and implicitly making it an exec operation, an optimization some but not all shells will implicitly perform), then yes, you have two shells.


To have your first shell exec the second one, replacing its image in memory and inheriting its PID, consider using the following line in your cron job:

exec /home/amit/Desktop/crontest/test.sh >/home/amit/Desktop/crontest/null 2>&1

Upvotes: 4

Related Questions