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.

  • 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

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

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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.