Bashrc, shell script and crontab
I'm trying to use the functions defined in /home/my_username/.bashrc in a shell script that gets executed by crontab.
#crontab -l
# m h dom mon dow command
* * * * * /bin/sh /home/my_username/CronTab_shell_script.sh >> /home/my_username/Desktop/file.log
Let's say I have the mounted() function saved in
/home/my_username/.bashrc
function mounted(){
if mount|grep $1; then
echo "mounted"
else
echo "not mounted"
fi
}
How can I call and use the mounted() function from the
/home/my_username/CronTab_shell_script.sh
bash scripts cron bashrc
add a comment |
I'm trying to use the functions defined in /home/my_username/.bashrc in a shell script that gets executed by crontab.
#crontab -l
# m h dom mon dow command
* * * * * /bin/sh /home/my_username/CronTab_shell_script.sh >> /home/my_username/Desktop/file.log
Let's say I have the mounted() function saved in
/home/my_username/.bashrc
function mounted(){
if mount|grep $1; then
echo "mounted"
else
echo "not mounted"
fi
}
How can I call and use the mounted() function from the
/home/my_username/CronTab_shell_script.sh
bash scripts cron bashrc
1
The easiest way is to put this function into the body ofCronTab_shell_script.sh:)
– pa4080
Sep 13 '17 at 12:09
add a comment |
I'm trying to use the functions defined in /home/my_username/.bashrc in a shell script that gets executed by crontab.
#crontab -l
# m h dom mon dow command
* * * * * /bin/sh /home/my_username/CronTab_shell_script.sh >> /home/my_username/Desktop/file.log
Let's say I have the mounted() function saved in
/home/my_username/.bashrc
function mounted(){
if mount|grep $1; then
echo "mounted"
else
echo "not mounted"
fi
}
How can I call and use the mounted() function from the
/home/my_username/CronTab_shell_script.sh
bash scripts cron bashrc
I'm trying to use the functions defined in /home/my_username/.bashrc in a shell script that gets executed by crontab.
#crontab -l
# m h dom mon dow command
* * * * * /bin/sh /home/my_username/CronTab_shell_script.sh >> /home/my_username/Desktop/file.log
Let's say I have the mounted() function saved in
/home/my_username/.bashrc
function mounted(){
if mount|grep $1; then
echo "mounted"
else
echo "not mounted"
fi
}
How can I call and use the mounted() function from the
/home/my_username/CronTab_shell_script.sh
bash scripts cron bashrc
bash scripts cron bashrc
edited Sep 13 '17 at 11:43
Yaron
9,22171941
9,22171941
asked Sep 13 '17 at 11:41
IDKIDK
188213
188213
1
The easiest way is to put this function into the body ofCronTab_shell_script.sh:)
– pa4080
Sep 13 '17 at 12:09
add a comment |
1
The easiest way is to put this function into the body ofCronTab_shell_script.sh:)
– pa4080
Sep 13 '17 at 12:09
1
1
The easiest way is to put this function into the body of
CronTab_shell_script.sh :)– pa4080
Sep 13 '17 at 12:09
The easiest way is to put this function into the body of
CronTab_shell_script.sh :)– pa4080
Sep 13 '17 at 12:09
add a comment |
2 Answers
2
active
oldest
votes
Cron runs with a limited shell and won't have access to your regular environment.
You could put source /home/my_username/.bashrc at the beginning of /home/my_username/CronTab_shell_script.sh to make the function available.
You could also source your .bashrc in your crontab:
#crontab -l
# m h dom mon dow command
* * * * * . /home/my_username/.bashrc; /bin/sh /home/my_username/CronTab_shell_script.sh >> home/my_username/Desktop/file.log
1
+1, but I think the Shell must bebash, it should be declared into the beginning of the script as#!/bin/bashand/bin/shshould be removed from the cron job. Also, maybe, the function should be exported after its definition in.bashrc:export -f mounted.
– pa4080
Sep 13 '17 at 12:15
add a comment |
1. Run the correct shell
* * * * * /bin/sh .....
You are not running bash, you are running sh, so you should not expect .bashrc to be loaded!
Try this instead:
* * * * * /bin/bash .....
2. Work around Ubuntu's default .bashrc
Another problem might be that Ubuntu's default .bashrc script starts with this guard:
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
Any lines you add below this will not be loaded by your cron script because cronjobs do not run in an interactive environment.
The solution: Put functions for scripts above this guard. Keep setup for the user below this guard.
3. Load the complete user login environment
On some systems I find its easiest to use --login to ensure the shell has the same things loaded that I have at the command line.
* * * * * bash --login /path/to/script.sh
* * * * * bash --login -c "your command here"
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f955415%2fbashrc-shell-script-and-crontab%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Cron runs with a limited shell and won't have access to your regular environment.
You could put source /home/my_username/.bashrc at the beginning of /home/my_username/CronTab_shell_script.sh to make the function available.
You could also source your .bashrc in your crontab:
#crontab -l
# m h dom mon dow command
* * * * * . /home/my_username/.bashrc; /bin/sh /home/my_username/CronTab_shell_script.sh >> home/my_username/Desktop/file.log
1
+1, but I think the Shell must bebash, it should be declared into the beginning of the script as#!/bin/bashand/bin/shshould be removed from the cron job. Also, maybe, the function should be exported after its definition in.bashrc:export -f mounted.
– pa4080
Sep 13 '17 at 12:15
add a comment |
Cron runs with a limited shell and won't have access to your regular environment.
You could put source /home/my_username/.bashrc at the beginning of /home/my_username/CronTab_shell_script.sh to make the function available.
You could also source your .bashrc in your crontab:
#crontab -l
# m h dom mon dow command
* * * * * . /home/my_username/.bashrc; /bin/sh /home/my_username/CronTab_shell_script.sh >> home/my_username/Desktop/file.log
1
+1, but I think the Shell must bebash, it should be declared into the beginning of the script as#!/bin/bashand/bin/shshould be removed from the cron job. Also, maybe, the function should be exported after its definition in.bashrc:export -f mounted.
– pa4080
Sep 13 '17 at 12:15
add a comment |
Cron runs with a limited shell and won't have access to your regular environment.
You could put source /home/my_username/.bashrc at the beginning of /home/my_username/CronTab_shell_script.sh to make the function available.
You could also source your .bashrc in your crontab:
#crontab -l
# m h dom mon dow command
* * * * * . /home/my_username/.bashrc; /bin/sh /home/my_username/CronTab_shell_script.sh >> home/my_username/Desktop/file.log
Cron runs with a limited shell and won't have access to your regular environment.
You could put source /home/my_username/.bashrc at the beginning of /home/my_username/CronTab_shell_script.sh to make the function available.
You could also source your .bashrc in your crontab:
#crontab -l
# m h dom mon dow command
* * * * * . /home/my_username/.bashrc; /bin/sh /home/my_username/CronTab_shell_script.sh >> home/my_username/Desktop/file.log
answered Sep 13 '17 at 11:55
GfeiGfei
212
212
1
+1, but I think the Shell must bebash, it should be declared into the beginning of the script as#!/bin/bashand/bin/shshould be removed from the cron job. Also, maybe, the function should be exported after its definition in.bashrc:export -f mounted.
– pa4080
Sep 13 '17 at 12:15
add a comment |
1
+1, but I think the Shell must bebash, it should be declared into the beginning of the script as#!/bin/bashand/bin/shshould be removed from the cron job. Also, maybe, the function should be exported after its definition in.bashrc:export -f mounted.
– pa4080
Sep 13 '17 at 12:15
1
1
+1, but I think the Shell must be
bash, it should be declared into the beginning of the script as #!/bin/bash and /bin/sh should be removed from the cron job. Also, maybe, the function should be exported after its definition in .bashrc : export -f mounted.– pa4080
Sep 13 '17 at 12:15
+1, but I think the Shell must be
bash, it should be declared into the beginning of the script as #!/bin/bash and /bin/sh should be removed from the cron job. Also, maybe, the function should be exported after its definition in .bashrc : export -f mounted.– pa4080
Sep 13 '17 at 12:15
add a comment |
1. Run the correct shell
* * * * * /bin/sh .....
You are not running bash, you are running sh, so you should not expect .bashrc to be loaded!
Try this instead:
* * * * * /bin/bash .....
2. Work around Ubuntu's default .bashrc
Another problem might be that Ubuntu's default .bashrc script starts with this guard:
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
Any lines you add below this will not be loaded by your cron script because cronjobs do not run in an interactive environment.
The solution: Put functions for scripts above this guard. Keep setup for the user below this guard.
3. Load the complete user login environment
On some systems I find its easiest to use --login to ensure the shell has the same things loaded that I have at the command line.
* * * * * bash --login /path/to/script.sh
* * * * * bash --login -c "your command here"
add a comment |
1. Run the correct shell
* * * * * /bin/sh .....
You are not running bash, you are running sh, so you should not expect .bashrc to be loaded!
Try this instead:
* * * * * /bin/bash .....
2. Work around Ubuntu's default .bashrc
Another problem might be that Ubuntu's default .bashrc script starts with this guard:
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
Any lines you add below this will not be loaded by your cron script because cronjobs do not run in an interactive environment.
The solution: Put functions for scripts above this guard. Keep setup for the user below this guard.
3. Load the complete user login environment
On some systems I find its easiest to use --login to ensure the shell has the same things loaded that I have at the command line.
* * * * * bash --login /path/to/script.sh
* * * * * bash --login -c "your command here"
add a comment |
1. Run the correct shell
* * * * * /bin/sh .....
You are not running bash, you are running sh, so you should not expect .bashrc to be loaded!
Try this instead:
* * * * * /bin/bash .....
2. Work around Ubuntu's default .bashrc
Another problem might be that Ubuntu's default .bashrc script starts with this guard:
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
Any lines you add below this will not be loaded by your cron script because cronjobs do not run in an interactive environment.
The solution: Put functions for scripts above this guard. Keep setup for the user below this guard.
3. Load the complete user login environment
On some systems I find its easiest to use --login to ensure the shell has the same things loaded that I have at the command line.
* * * * * bash --login /path/to/script.sh
* * * * * bash --login -c "your command here"
1. Run the correct shell
* * * * * /bin/sh .....
You are not running bash, you are running sh, so you should not expect .bashrc to be loaded!
Try this instead:
* * * * * /bin/bash .....
2. Work around Ubuntu's default .bashrc
Another problem might be that Ubuntu's default .bashrc script starts with this guard:
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
Any lines you add below this will not be loaded by your cron script because cronjobs do not run in an interactive environment.
The solution: Put functions for scripts above this guard. Keep setup for the user below this guard.
3. Load the complete user login environment
On some systems I find its easiest to use --login to ensure the shell has the same things loaded that I have at the command line.
* * * * * bash --login /path/to/script.sh
* * * * * bash --login -c "your command here"
edited Feb 26 at 6:29
answered Oct 4 '18 at 8:30
joeytwiddlejoeytwiddle
1,0241021
1,0241021
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f955415%2fbashrc-shell-script-and-crontab%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
The easiest way is to put this function into the body of
CronTab_shell_script.sh:)– pa4080
Sep 13 '17 at 12:09