pkill -P variable not working when running .sh under cron
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a very simple .sh
script to kill a process. The processes saves its PID to pid2.txt
.
pkill -e -P $(cat pid2.txt)
exit
When I run this as sudo ./myscript.sh
from the command line it works fine.
When run by cron as */2 * * * * /etc/mylocation/myscript.sh
it throws an error:
cat: pid2.txt: No such file or directory pkill: option requires an argument -- 'P'
What am I doing wrong?
bash cron pkill
add a comment |Â
up vote
0
down vote
favorite
I have a very simple .sh
script to kill a process. The processes saves its PID to pid2.txt
.
pkill -e -P $(cat pid2.txt)
exit
When I run this as sudo ./myscript.sh
from the command line it works fine.
When run by cron as */2 * * * * /etc/mylocation/myscript.sh
it throws an error:
cat: pid2.txt: No such file or directory pkill: option requires an argument -- 'P'
What am I doing wrong?
bash cron pkill
3
Add the full path ofpid2.txt
in your script, it will then work
â Arushix
Jun 21 at 5:32
Great, I will add it as the answer
â Arushix
Jun 21 at 5:42
I'm unfamiliar with the-e
flag forpkill
. What does it do on your system, and what Unix are you using?
â Kusalananda
Jun 21 at 6:02
Ubuntu 16.04. -e echoes which PID was killed.
â sjaak
Jun 21 at 7:02
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a very simple .sh
script to kill a process. The processes saves its PID to pid2.txt
.
pkill -e -P $(cat pid2.txt)
exit
When I run this as sudo ./myscript.sh
from the command line it works fine.
When run by cron as */2 * * * * /etc/mylocation/myscript.sh
it throws an error:
cat: pid2.txt: No such file or directory pkill: option requires an argument -- 'P'
What am I doing wrong?
bash cron pkill
I have a very simple .sh
script to kill a process. The processes saves its PID to pid2.txt
.
pkill -e -P $(cat pid2.txt)
exit
When I run this as sudo ./myscript.sh
from the command line it works fine.
When run by cron as */2 * * * * /etc/mylocation/myscript.sh
it throws an error:
cat: pid2.txt: No such file or directory pkill: option requires an argument -- 'P'
What am I doing wrong?
bash cron pkill
edited Jun 21 at 8:57
Anthony Geoghegan
7,15233651
7,15233651
asked Jun 21 at 5:29
sjaak
182
182
3
Add the full path ofpid2.txt
in your script, it will then work
â Arushix
Jun 21 at 5:32
Great, I will add it as the answer
â Arushix
Jun 21 at 5:42
I'm unfamiliar with the-e
flag forpkill
. What does it do on your system, and what Unix are you using?
â Kusalananda
Jun 21 at 6:02
Ubuntu 16.04. -e echoes which PID was killed.
â sjaak
Jun 21 at 7:02
add a comment |Â
3
Add the full path ofpid2.txt
in your script, it will then work
â Arushix
Jun 21 at 5:32
Great, I will add it as the answer
â Arushix
Jun 21 at 5:42
I'm unfamiliar with the-e
flag forpkill
. What does it do on your system, and what Unix are you using?
â Kusalananda
Jun 21 at 6:02
Ubuntu 16.04. -e echoes which PID was killed.
â sjaak
Jun 21 at 7:02
3
3
Add the full path of
pid2.txt
in your script, it will then workâ Arushix
Jun 21 at 5:32
Add the full path of
pid2.txt
in your script, it will then workâ Arushix
Jun 21 at 5:32
Great, I will add it as the answer
â Arushix
Jun 21 at 5:42
Great, I will add it as the answer
â Arushix
Jun 21 at 5:42
I'm unfamiliar with the
-e
flag for pkill
. What does it do on your system, and what Unix are you using?â Kusalananda
Jun 21 at 6:02
I'm unfamiliar with the
-e
flag for pkill
. What does it do on your system, and what Unix are you using?â Kusalananda
Jun 21 at 6:02
Ubuntu 16.04. -e echoes which PID was killed.
â sjaak
Jun 21 at 7:02
Ubuntu 16.04. -e echoes which PID was killed.
â sjaak
Jun 21 at 7:02
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
The cron job is not necessarily running with the same working directory that you happen to have in the interactive session. This means that the file pid2.txt
might not be available where you think it is. Change the script so that it uses an absolute path to the pid2.txt
file.
You may also remove exit
from the end of the script. It serves no purpose.
add a comment |Â
up vote
0
down vote
Add the full path of pid2.txt
in your script, it will then work
Explanation:
cron jobs run in a very minimal environment, and since they're executed directly by crond without a shell (unless you force one to be created), the regular shell setup never happens. Hence you need to explicitly specify path of all files, scripts being called by cron
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
The cron job is not necessarily running with the same working directory that you happen to have in the interactive session. This means that the file pid2.txt
might not be available where you think it is. Change the script so that it uses an absolute path to the pid2.txt
file.
You may also remove exit
from the end of the script. It serves no purpose.
add a comment |Â
up vote
1
down vote
The cron job is not necessarily running with the same working directory that you happen to have in the interactive session. This means that the file pid2.txt
might not be available where you think it is. Change the script so that it uses an absolute path to the pid2.txt
file.
You may also remove exit
from the end of the script. It serves no purpose.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
The cron job is not necessarily running with the same working directory that you happen to have in the interactive session. This means that the file pid2.txt
might not be available where you think it is. Change the script so that it uses an absolute path to the pid2.txt
file.
You may also remove exit
from the end of the script. It serves no purpose.
The cron job is not necessarily running with the same working directory that you happen to have in the interactive session. This means that the file pid2.txt
might not be available where you think it is. Change the script so that it uses an absolute path to the pid2.txt
file.
You may also remove exit
from the end of the script. It serves no purpose.
edited Jun 21 at 6:19
answered Jun 21 at 5:56
Kusalananda
101k13199312
101k13199312
add a comment |Â
add a comment |Â
up vote
0
down vote
Add the full path of pid2.txt
in your script, it will then work
Explanation:
cron jobs run in a very minimal environment, and since they're executed directly by crond without a shell (unless you force one to be created), the regular shell setup never happens. Hence you need to explicitly specify path of all files, scripts being called by cron
add a comment |Â
up vote
0
down vote
Add the full path of pid2.txt
in your script, it will then work
Explanation:
cron jobs run in a very minimal environment, and since they're executed directly by crond without a shell (unless you force one to be created), the regular shell setup never happens. Hence you need to explicitly specify path of all files, scripts being called by cron
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Add the full path of pid2.txt
in your script, it will then work
Explanation:
cron jobs run in a very minimal environment, and since they're executed directly by crond without a shell (unless you force one to be created), the regular shell setup never happens. Hence you need to explicitly specify path of all files, scripts being called by cron
Add the full path of pid2.txt
in your script, it will then work
Explanation:
cron jobs run in a very minimal environment, and since they're executed directly by crond without a shell (unless you force one to be created), the regular shell setup never happens. Hence you need to explicitly specify path of all files, scripts being called by cron
answered Jun 21 at 6:09
Arushix
9968
9968
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f451020%2fpkill-p-variable-not-working-when-running-sh-under-cron%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
3
Add the full path of
pid2.txt
in your script, it will then workâ Arushix
Jun 21 at 5:32
Great, I will add it as the answer
â Arushix
Jun 21 at 5:42
I'm unfamiliar with the
-e
flag forpkill
. What does it do on your system, and what Unix are you using?â Kusalananda
Jun 21 at 6:02
Ubuntu 16.04. -e echoes which PID was killed.
â sjaak
Jun 21 at 7:02