Specify shell for individual cron job
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I have a cron job I need to run as root and that needs bash rather than the default sh shell.
The root
crontab has other jobs in it that run fine as they are and I'd rather not risk breaking those by specifying SHELL=/bin/bash
at the top of the crontab
file.
How can I specify my cron job is to be run using bash without changing the shell used for other existing jobs?
I'm kind of thinking that a line like 0 * * * * SHELL=/bin/bash myjob
might do the trick, but I can't find that mentioned anywhere in the CentOS documentation for cron.
I also thought that maybe setting the SHELL variable immediately before my job (which would be the last one in the file) might work, but couldn't find an documentation of the effect of placement of environment variables and whether they were assigned in document order or were just considered global regardless of their position (or perhaps generated an error if not before all jobs).
Is it possible for me to specify the shell on a job-by-job basis?
bash centos cron
add a comment |Â
up vote
2
down vote
favorite
I have a cron job I need to run as root and that needs bash rather than the default sh shell.
The root
crontab has other jobs in it that run fine as they are and I'd rather not risk breaking those by specifying SHELL=/bin/bash
at the top of the crontab
file.
How can I specify my cron job is to be run using bash without changing the shell used for other existing jobs?
I'm kind of thinking that a line like 0 * * * * SHELL=/bin/bash myjob
might do the trick, but I can't find that mentioned anywhere in the CentOS documentation for cron.
I also thought that maybe setting the SHELL variable immediately before my job (which would be the last one in the file) might work, but couldn't find an documentation of the effect of placement of environment variables and whether they were assigned in document order or were just considered global regardless of their position (or perhaps generated an error if not before all jobs).
Is it possible for me to specify the shell on a job-by-job basis?
bash centos cron
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a cron job I need to run as root and that needs bash rather than the default sh shell.
The root
crontab has other jobs in it that run fine as they are and I'd rather not risk breaking those by specifying SHELL=/bin/bash
at the top of the crontab
file.
How can I specify my cron job is to be run using bash without changing the shell used for other existing jobs?
I'm kind of thinking that a line like 0 * * * * SHELL=/bin/bash myjob
might do the trick, but I can't find that mentioned anywhere in the CentOS documentation for cron.
I also thought that maybe setting the SHELL variable immediately before my job (which would be the last one in the file) might work, but couldn't find an documentation of the effect of placement of environment variables and whether they were assigned in document order or were just considered global regardless of their position (or perhaps generated an error if not before all jobs).
Is it possible for me to specify the shell on a job-by-job basis?
bash centos cron
I have a cron job I need to run as root and that needs bash rather than the default sh shell.
The root
crontab has other jobs in it that run fine as they are and I'd rather not risk breaking those by specifying SHELL=/bin/bash
at the top of the crontab
file.
How can I specify my cron job is to be run using bash without changing the shell used for other existing jobs?
I'm kind of thinking that a line like 0 * * * * SHELL=/bin/bash myjob
might do the trick, but I can't find that mentioned anywhere in the CentOS documentation for cron.
I also thought that maybe setting the SHELL variable immediately before my job (which would be the last one in the file) might work, but couldn't find an documentation of the effect of placement of environment variables and whether they were assigned in document order or were just considered global regardless of their position (or perhaps generated an error if not before all jobs).
Is it possible for me to specify the shell on a job-by-job basis?
bash centos cron
asked Mar 15 at 19:16
scanny
1112
1112
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
5
down vote
The most straight-forward way to specify the shell is simply to invoke that shell's executable file. For example, if you want to execute a command called myjob
using bash
, you could use something like the following command-line:
/bin/bash -c 'myjob'
The corresponding crontab entry might look like this:
0 * * * * /bin/bash -c 'myjob'
Alternatively, if myjob
is a script, you might use something like the following command-line instead:
/bin/bash '/path/to/myjob'
And the corresponding crontab entry for that would be something like this:
0 * * * * /bin/bash '/path/to/myjob'
You can see, for example in cronie, that you can override SHELL (once), otherwise it picks up the default shell, which is set to /bin/sh
â Jeff Schaller
Mar 15 at 20:25
@scanny Does this solution not resolve your issue? If not, could you specify what's lacking from this solution?
â igal
Mar 26 at 3:11
add a comment |Â
up vote
1
down vote
If myjob
is a shell script, make sure that it is executable and that it's #!
-line specifies the correct interpreter for the script file (e.g. #!/bin/bash
). That's all.
If it's a series of commands, I'd recommend putting those in a bash
script, with a proper #!
-line, as above.
This way, you don't really have to worry too much about the particularities of getting the crontab entry correct (apart from at what times to run the script). It also removes the need to update the cronjob when you reimplement the script in Python or Perl or some other shell variant.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
The most straight-forward way to specify the shell is simply to invoke that shell's executable file. For example, if you want to execute a command called myjob
using bash
, you could use something like the following command-line:
/bin/bash -c 'myjob'
The corresponding crontab entry might look like this:
0 * * * * /bin/bash -c 'myjob'
Alternatively, if myjob
is a script, you might use something like the following command-line instead:
/bin/bash '/path/to/myjob'
And the corresponding crontab entry for that would be something like this:
0 * * * * /bin/bash '/path/to/myjob'
You can see, for example in cronie, that you can override SHELL (once), otherwise it picks up the default shell, which is set to /bin/sh
â Jeff Schaller
Mar 15 at 20:25
@scanny Does this solution not resolve your issue? If not, could you specify what's lacking from this solution?
â igal
Mar 26 at 3:11
add a comment |Â
up vote
5
down vote
The most straight-forward way to specify the shell is simply to invoke that shell's executable file. For example, if you want to execute a command called myjob
using bash
, you could use something like the following command-line:
/bin/bash -c 'myjob'
The corresponding crontab entry might look like this:
0 * * * * /bin/bash -c 'myjob'
Alternatively, if myjob
is a script, you might use something like the following command-line instead:
/bin/bash '/path/to/myjob'
And the corresponding crontab entry for that would be something like this:
0 * * * * /bin/bash '/path/to/myjob'
You can see, for example in cronie, that you can override SHELL (once), otherwise it picks up the default shell, which is set to /bin/sh
â Jeff Schaller
Mar 15 at 20:25
@scanny Does this solution not resolve your issue? If not, could you specify what's lacking from this solution?
â igal
Mar 26 at 3:11
add a comment |Â
up vote
5
down vote
up vote
5
down vote
The most straight-forward way to specify the shell is simply to invoke that shell's executable file. For example, if you want to execute a command called myjob
using bash
, you could use something like the following command-line:
/bin/bash -c 'myjob'
The corresponding crontab entry might look like this:
0 * * * * /bin/bash -c 'myjob'
Alternatively, if myjob
is a script, you might use something like the following command-line instead:
/bin/bash '/path/to/myjob'
And the corresponding crontab entry for that would be something like this:
0 * * * * /bin/bash '/path/to/myjob'
The most straight-forward way to specify the shell is simply to invoke that shell's executable file. For example, if you want to execute a command called myjob
using bash
, you could use something like the following command-line:
/bin/bash -c 'myjob'
The corresponding crontab entry might look like this:
0 * * * * /bin/bash -c 'myjob'
Alternatively, if myjob
is a script, you might use something like the following command-line instead:
/bin/bash '/path/to/myjob'
And the corresponding crontab entry for that would be something like this:
0 * * * * /bin/bash '/path/to/myjob'
edited Mar 15 at 22:09
answered Mar 15 at 20:02
igal
4,820930
4,820930
You can see, for example in cronie, that you can override SHELL (once), otherwise it picks up the default shell, which is set to /bin/sh
â Jeff Schaller
Mar 15 at 20:25
@scanny Does this solution not resolve your issue? If not, could you specify what's lacking from this solution?
â igal
Mar 26 at 3:11
add a comment |Â
You can see, for example in cronie, that you can override SHELL (once), otherwise it picks up the default shell, which is set to /bin/sh
â Jeff Schaller
Mar 15 at 20:25
@scanny Does this solution not resolve your issue? If not, could you specify what's lacking from this solution?
â igal
Mar 26 at 3:11
You can see, for example in cronie, that you can override SHELL (once), otherwise it picks up the default shell, which is set to /bin/sh
â Jeff Schaller
Mar 15 at 20:25
You can see, for example in cronie, that you can override SHELL (once), otherwise it picks up the default shell, which is set to /bin/sh
â Jeff Schaller
Mar 15 at 20:25
@scanny Does this solution not resolve your issue? If not, could you specify what's lacking from this solution?
â igal
Mar 26 at 3:11
@scanny Does this solution not resolve your issue? If not, could you specify what's lacking from this solution?
â igal
Mar 26 at 3:11
add a comment |Â
up vote
1
down vote
If myjob
is a shell script, make sure that it is executable and that it's #!
-line specifies the correct interpreter for the script file (e.g. #!/bin/bash
). That's all.
If it's a series of commands, I'd recommend putting those in a bash
script, with a proper #!
-line, as above.
This way, you don't really have to worry too much about the particularities of getting the crontab entry correct (apart from at what times to run the script). It also removes the need to update the cronjob when you reimplement the script in Python or Perl or some other shell variant.
add a comment |Â
up vote
1
down vote
If myjob
is a shell script, make sure that it is executable and that it's #!
-line specifies the correct interpreter for the script file (e.g. #!/bin/bash
). That's all.
If it's a series of commands, I'd recommend putting those in a bash
script, with a proper #!
-line, as above.
This way, you don't really have to worry too much about the particularities of getting the crontab entry correct (apart from at what times to run the script). It also removes the need to update the cronjob when you reimplement the script in Python or Perl or some other shell variant.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
If myjob
is a shell script, make sure that it is executable and that it's #!
-line specifies the correct interpreter for the script file (e.g. #!/bin/bash
). That's all.
If it's a series of commands, I'd recommend putting those in a bash
script, with a proper #!
-line, as above.
This way, you don't really have to worry too much about the particularities of getting the crontab entry correct (apart from at what times to run the script). It also removes the need to update the cronjob when you reimplement the script in Python or Perl or some other shell variant.
If myjob
is a shell script, make sure that it is executable and that it's #!
-line specifies the correct interpreter for the script file (e.g. #!/bin/bash
). That's all.
If it's a series of commands, I'd recommend putting those in a bash
script, with a proper #!
-line, as above.
This way, you don't really have to worry too much about the particularities of getting the crontab entry correct (apart from at what times to run the script). It also removes the need to update the cronjob when you reimplement the script in Python or Perl or some other shell variant.
answered Apr 13 at 10:52
Kusalananda
103k13201317
103k13201317
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%2f430466%2fspecify-shell-for-individual-cron-job%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