Bash script able to run manually, but with cron not working. + Ubuntu 16.04

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0














Env:- Ubuntu 18.04



I write one C program and trying to speak to port and fetch some data and dump into one file. Then I created one bash script and added this C program and expect to run at multiple intervals.



I'm able to run this bash script without any issues. I'm running as root.



<<snip>>
#!/bin/bash
interval=$1
time=$2
./CC-test $interval $time
<<snip>>


May I know is there any permission delegation issue for invoking some commands via cron? or do we need to tell cron to excecute with administrative privilege? Anyway I'm running cron as root, then I don't think so, if anything other required.



As a test, I just tried two commands in a shell script as follows



 #!/bin/bash
date >> test
fdisk -l >> test


Even here also I can able to run manually and even both output is printing without any issues. For here I put it is in cron, on the "date" command output which is printed in test file.



Please shed me some views on this.










share|improve this question





















  • Are you running date in the cron job itself? Maybe show that cron entry as well...
    – Jeff Schaller
    Dec 20 '18 at 11:11










  • What happens if you do env -i ./testscript - this can frequently show hidden problems that don't show when run from the command line but may show when run from cron.
    – Stephen Harris
    Dec 20 '18 at 15:05















0














Env:- Ubuntu 18.04



I write one C program and trying to speak to port and fetch some data and dump into one file. Then I created one bash script and added this C program and expect to run at multiple intervals.



I'm able to run this bash script without any issues. I'm running as root.



<<snip>>
#!/bin/bash
interval=$1
time=$2
./CC-test $interval $time
<<snip>>


May I know is there any permission delegation issue for invoking some commands via cron? or do we need to tell cron to excecute with administrative privilege? Anyway I'm running cron as root, then I don't think so, if anything other required.



As a test, I just tried two commands in a shell script as follows



 #!/bin/bash
date >> test
fdisk -l >> test


Even here also I can able to run manually and even both output is printing without any issues. For here I put it is in cron, on the "date" command output which is printed in test file.



Please shed me some views on this.










share|improve this question





















  • Are you running date in the cron job itself? Maybe show that cron entry as well...
    – Jeff Schaller
    Dec 20 '18 at 11:11










  • What happens if you do env -i ./testscript - this can frequently show hidden problems that don't show when run from the command line but may show when run from cron.
    – Stephen Harris
    Dec 20 '18 at 15:05













0












0








0







Env:- Ubuntu 18.04



I write one C program and trying to speak to port and fetch some data and dump into one file. Then I created one bash script and added this C program and expect to run at multiple intervals.



I'm able to run this bash script without any issues. I'm running as root.



<<snip>>
#!/bin/bash
interval=$1
time=$2
./CC-test $interval $time
<<snip>>


May I know is there any permission delegation issue for invoking some commands via cron? or do we need to tell cron to excecute with administrative privilege? Anyway I'm running cron as root, then I don't think so, if anything other required.



As a test, I just tried two commands in a shell script as follows



 #!/bin/bash
date >> test
fdisk -l >> test


Even here also I can able to run manually and even both output is printing without any issues. For here I put it is in cron, on the "date" command output which is printed in test file.



Please shed me some views on this.










share|improve this question













Env:- Ubuntu 18.04



I write one C program and trying to speak to port and fetch some data and dump into one file. Then I created one bash script and added this C program and expect to run at multiple intervals.



I'm able to run this bash script without any issues. I'm running as root.



<<snip>>
#!/bin/bash
interval=$1
time=$2
./CC-test $interval $time
<<snip>>


May I know is there any permission delegation issue for invoking some commands via cron? or do we need to tell cron to excecute with administrative privilege? Anyway I'm running cron as root, then I don't think so, if anything other required.



As a test, I just tried two commands in a shell script as follows



 #!/bin/bash
date >> test
fdisk -l >> test


Even here also I can able to run manually and even both output is printing without any issues. For here I put it is in cron, on the "date" command output which is printed in test file.



Please shed me some views on this.







linux ubuntu cron






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 20 '18 at 11:10









user183980

31




31











  • Are you running date in the cron job itself? Maybe show that cron entry as well...
    – Jeff Schaller
    Dec 20 '18 at 11:11










  • What happens if you do env -i ./testscript - this can frequently show hidden problems that don't show when run from the command line but may show when run from cron.
    – Stephen Harris
    Dec 20 '18 at 15:05
















  • Are you running date in the cron job itself? Maybe show that cron entry as well...
    – Jeff Schaller
    Dec 20 '18 at 11:11










  • What happens if you do env -i ./testscript - this can frequently show hidden problems that don't show when run from the command line but may show when run from cron.
    – Stephen Harris
    Dec 20 '18 at 15:05















Are you running date in the cron job itself? Maybe show that cron entry as well...
– Jeff Schaller
Dec 20 '18 at 11:11




Are you running date in the cron job itself? Maybe show that cron entry as well...
– Jeff Schaller
Dec 20 '18 at 11:11












What happens if you do env -i ./testscript - this can frequently show hidden problems that don't show when run from the command line but may show when run from cron.
– Stephen Harris
Dec 20 '18 at 15:05




What happens if you do env -i ./testscript - this can frequently show hidden problems that don't show when run from the command line but may show when run from cron.
– Stephen Harris
Dec 20 '18 at 15:05










1 Answer
1






active

oldest

votes


















0














The cron daemon is always running as root. The cron jobs will be run as the user whose cronjob they belong to. If you add the cron job with crontab -e as user john, then the job will be running as user john, not as root.



To edit root's cron jobs, use sudo crontab -e.



The difference between running a command from the interactive command line and from a cron job is that the environment (environment variables, current working directory etc.) may well be different. Ideally, a cron job should explicitly set the needed variables to the correct values. For example, a script executed from cron may want to add a few paths the PATH variable if 3rd-party utilities are used from non-standard paths, and it may want to cd into the correct directory to set the working directory for the rest of the script (so that you, in your example, are in the correct directory when you run ./CC-test, for example).






share|improve this answer






















  • @user183980 If the cron job produces errors, you would get them emailed to you (root). You could also try to redirect the error stream to the file to see them there with fdisk -l >>test 2>&1. It was not clear from the question what worked and what did not work.
    – Kusalananda
    Dec 20 '18 at 11:51










  • I suspect the path could be the problem, let me retest again. thanks for your suggestion
    – user183980
    Dec 20 '18 at 11:53










Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f490106%2fbash-script-able-to-run-manually-but-with-cron-not-working-ubuntu-16-04%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The cron daemon is always running as root. The cron jobs will be run as the user whose cronjob they belong to. If you add the cron job with crontab -e as user john, then the job will be running as user john, not as root.



To edit root's cron jobs, use sudo crontab -e.



The difference between running a command from the interactive command line and from a cron job is that the environment (environment variables, current working directory etc.) may well be different. Ideally, a cron job should explicitly set the needed variables to the correct values. For example, a script executed from cron may want to add a few paths the PATH variable if 3rd-party utilities are used from non-standard paths, and it may want to cd into the correct directory to set the working directory for the rest of the script (so that you, in your example, are in the correct directory when you run ./CC-test, for example).






share|improve this answer






















  • @user183980 If the cron job produces errors, you would get them emailed to you (root). You could also try to redirect the error stream to the file to see them there with fdisk -l >>test 2>&1. It was not clear from the question what worked and what did not work.
    – Kusalananda
    Dec 20 '18 at 11:51










  • I suspect the path could be the problem, let me retest again. thanks for your suggestion
    – user183980
    Dec 20 '18 at 11:53















0














The cron daemon is always running as root. The cron jobs will be run as the user whose cronjob they belong to. If you add the cron job with crontab -e as user john, then the job will be running as user john, not as root.



To edit root's cron jobs, use sudo crontab -e.



The difference between running a command from the interactive command line and from a cron job is that the environment (environment variables, current working directory etc.) may well be different. Ideally, a cron job should explicitly set the needed variables to the correct values. For example, a script executed from cron may want to add a few paths the PATH variable if 3rd-party utilities are used from non-standard paths, and it may want to cd into the correct directory to set the working directory for the rest of the script (so that you, in your example, are in the correct directory when you run ./CC-test, for example).






share|improve this answer






















  • @user183980 If the cron job produces errors, you would get them emailed to you (root). You could also try to redirect the error stream to the file to see them there with fdisk -l >>test 2>&1. It was not clear from the question what worked and what did not work.
    – Kusalananda
    Dec 20 '18 at 11:51










  • I suspect the path could be the problem, let me retest again. thanks for your suggestion
    – user183980
    Dec 20 '18 at 11:53













0












0








0






The cron daemon is always running as root. The cron jobs will be run as the user whose cronjob they belong to. If you add the cron job with crontab -e as user john, then the job will be running as user john, not as root.



To edit root's cron jobs, use sudo crontab -e.



The difference between running a command from the interactive command line and from a cron job is that the environment (environment variables, current working directory etc.) may well be different. Ideally, a cron job should explicitly set the needed variables to the correct values. For example, a script executed from cron may want to add a few paths the PATH variable if 3rd-party utilities are used from non-standard paths, and it may want to cd into the correct directory to set the working directory for the rest of the script (so that you, in your example, are in the correct directory when you run ./CC-test, for example).






share|improve this answer














The cron daemon is always running as root. The cron jobs will be run as the user whose cronjob they belong to. If you add the cron job with crontab -e as user john, then the job will be running as user john, not as root.



To edit root's cron jobs, use sudo crontab -e.



The difference between running a command from the interactive command line and from a cron job is that the environment (environment variables, current working directory etc.) may well be different. Ideally, a cron job should explicitly set the needed variables to the correct values. For example, a script executed from cron may want to add a few paths the PATH variable if 3rd-party utilities are used from non-standard paths, and it may want to cd into the correct directory to set the working directory for the rest of the script (so that you, in your example, are in the correct directory when you run ./CC-test, for example).







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 20 '18 at 11:29

























answered Dec 20 '18 at 11:22









Kusalananda

121k16229372




121k16229372











  • @user183980 If the cron job produces errors, you would get them emailed to you (root). You could also try to redirect the error stream to the file to see them there with fdisk -l >>test 2>&1. It was not clear from the question what worked and what did not work.
    – Kusalananda
    Dec 20 '18 at 11:51










  • I suspect the path could be the problem, let me retest again. thanks for your suggestion
    – user183980
    Dec 20 '18 at 11:53
















  • @user183980 If the cron job produces errors, you would get them emailed to you (root). You could also try to redirect the error stream to the file to see them there with fdisk -l >>test 2>&1. It was not clear from the question what worked and what did not work.
    – Kusalananda
    Dec 20 '18 at 11:51










  • I suspect the path could be the problem, let me retest again. thanks for your suggestion
    – user183980
    Dec 20 '18 at 11:53















@user183980 If the cron job produces errors, you would get them emailed to you (root). You could also try to redirect the error stream to the file to see them there with fdisk -l >>test 2>&1. It was not clear from the question what worked and what did not work.
– Kusalananda
Dec 20 '18 at 11:51




@user183980 If the cron job produces errors, you would get them emailed to you (root). You could also try to redirect the error stream to the file to see them there with fdisk -l >>test 2>&1. It was not clear from the question what worked and what did not work.
– Kusalananda
Dec 20 '18 at 11:51












I suspect the path could be the problem, let me retest again. thanks for your suggestion
– user183980
Dec 20 '18 at 11:53




I suspect the path could be the problem, let me retest again. thanks for your suggestion
– user183980
Dec 20 '18 at 11:53

















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • 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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f490106%2fbash-script-able-to-run-manually-but-with-cron-not-working-ubuntu-16-04%23new-answer', 'question_page');

);

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






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay