doubleudee1
Posts: 164
Joined: Fri Nov 30, 2018 2:39 pm
Location: Wales

How to run an executable file from CRON?

Fri Apr 12, 2019 10:19 am

I have a couple of executable files in different directories that I would like to run at a set time, I have already got a cron file which I have a couple of commands I run in the mornings but how do I run an executable at a set time within the same cron file?
e.g. my executable is in /dir1/dir2/dir3/ executable-file-name

If I wanted it to run at 1am daily I would I put something like '0 1 * * * ./executable-file-name /dir1/dir2/dir3
or would I have to append the user (I am using pi) somewhere within the executable line?

Never ran one from a cron or anything before other than manually using ./executable-file-name.

Thanks
Make the most of your family and friends , your children grow up too quickly and you don't notice yourself ageing, friends dissapearing, moving on. You make plans for your future, but they can all be smashed in an instant, live life and enjoy. :)

markkuk
Posts: 244
Joined: Thu Mar 22, 2018 1:02 pm
Location: Finland

Re: How to run an executable file from CRON?

Fri Apr 12, 2019 11:17 am

The correct cron line would be:

Code: Select all

0 1 * * * /dir1/dir2/dir3/executable-file-name
Each user has their own crontab file that can be edited by the command "crontab -e", so you don't need to specify the username.

User avatar
thagrol
Posts: 4018
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: How to run an executable file from CRON?

Fri Apr 12, 2019 11:18 am

doubleudee1 wrote:
Fri Apr 12, 2019 10:19 am
I have a couple of executable files in different directories that I would like to run at a set time, I have already got a cron file which I have a couple of commands I run in the mornings but how do I run an executable at a set time within the same cron file?
e.g. my executable is in /dir1/dir2/dir3/ executable-file-name

If I wanted it to run at 1am daily I would I put something like '0 1 * * * ./executable-file-name /dir1/dir2/dir3
or would I have to append the user (I am using pi) somewhere within the executable line?

Never ran one from a cron or anything before other than manually using ./executable-file-name.

Thanks
The key thing is to use the full path to everything. For a script named bar, in the foo subdirectory of the Pi with the directory /tmp/baz as its parameter you'd do something like this:

Code: Select all

0 1 * * *  /home/pi/foo/bar /tmp/baz
It's probably sensible to capture any output and error messages just in case so:

Code: Select all

0 1 * * *  /home/pi/foo/bar /tmp/baz >/tmp/bar.log 2>&1
Arguing with strangers on the internet since 1993.

scotty101
Posts: 4151
Joined: Fri Jun 08, 2012 6:03 pm

Re: How to run an executable file from CRON?

Fri Apr 12, 2019 11:21 am

doubleudee1 wrote:
Fri Apr 12, 2019 10:19 am
would I put something like '0 1 * * * ./executable-file-name /dir1/dir2/dir3
Don't think so.

Its something like

Code: Select all

'0 1 * * * /dir1/dir2/dir3/executable-file-name 
For example

Code: Select all

0 1 * * * /home/pi/myDirectory/myProgram
Remember your binary will have to be made executable using chmod +x myProgram
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

tpyo kingg
Posts: 971
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: How to run an executable file from CRON?

Fri Apr 12, 2019 11:22 am

You would remove the dot to have an absolute path, not a relative path. That is to say, use /dir1/dir2/dir3/program

As to specifying the account name, that depends on how you set up the cron job. If you use 'crontab -e' under the account 'pi' then it should not be added. If you edit the system-wide, then you must also specify which account the script will be run as.

See "man 5 crontab" for the details.

doubleudee1
Posts: 164
Joined: Fri Nov 30, 2018 2:39 pm
Location: Wales

Re: How to run an executable file from CRON?

Fri Apr 12, 2019 12:04 pm

Thanks all
Thats cleared it up for me.

WD
Make the most of your family and friends , your children grow up too quickly and you don't notice yourself ageing, friends dissapearing, moving on. You make plans for your future, but they can all be smashed in an instant, live life and enjoy. :)

Return to “Beginners”