1

This question already has an answer here:

I have setup a VPS with Ubuntu 12.04 and now I want to run a cronjob once a day.

The command line is as follows:

    mono /root/Folder/Aplication.exe

I've looked and looked around the internet but cant seem to understand how to do it. I for instance want to run this command every 24 hours at midnight.

Thank you in advance for reading my question and of course for all the answers.

marked as duplicate by Tom Brossman, Kevin Bowen, Zanna, David Foerster, Eric Carvalho Jan 9 '17 at 16:13

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

Because there are some complementary differences between @Guss's and my answer, I will leave it.

Here you are an illustration about cronjobs setup (source):

* * * * * command to be executed
- - - - -
| | | | |
| | | | +----- day of week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)
  • If you want to run a job by your $USER every day at 0:15 o'clock, you need to edit user's crontab file (which is located in /var/spool/cron/crontabs/<user_name>). You can do that by the command crontab -e. (Maybe there will be appeared a dialog, where you must choose the default terminal text editor.) In the end of the crontab file add next line:

    15 0 * * * /usr/bin/mono /root/Folder/Aplication.exe > /var/log/mono.daily.log 2>&1
    
    • /usr/bin/mono it's recommended to use full path to the command in Cron. To find it you can use sudo updatedb && sudo whereis mono.
    • > /var/log/mono.daily.log this part will redirect the output to a log file, in case you want to trace what is happen with your cronjob. If you want to keep log data from the previous days, you can use >> instead of >.
    • 2>&1 this part will put the error messages into the log file.
  • If you want to run a job by root, you must edit its crontab file: sudocrontab -e and add the line shown above.

  • Also, for daily cronjobs, you can add an executable file in the folder /etc/cron.daily. To find an example, check some of existing files into this folder.

  • 2
    I don't normally specify where the user cron tables are stored because some people are tempted to edit them directly instead of using crontab -e - and that can cause all kinds of trouble, not the least of which that the cron daemon will not refresh these files automatically, like it does the system cron tables. – Guss Jan 8 '17 at 19:33
  • It is good to know where these files are stored, if you want to make a backup of customised files of your system. – pa4080 Jan 8 '17 at 19:37
1

You run scheduled jobs in Ubuntu (and other Linuxes) using cron. One thing that is important to remember about cron, is that there are "user cron tables" and "system cron tables".

If you want your application to run in the context of a user, then its relatively simple: log in as that user and run crontab -e. You'd get into an editor where you edit the cron table manually.

Under a user cron table, you have 6 fields:

<minute> <hour> <day-of-month> <month> <day-of-week> <command ...>

The field are space separated except "command" which extends to the end of the line (with some caveats). Read man 5 crontab for the gory details.

To run something every day, you might want to choose a time, then program that into the first two fields, leaving all the other fields as asterisk (i.e. "anything goes"). So the expression

1 2 * * * mono /root/Folder/Aplication.exe

Would run your mono application every day (every day of every month regardless of the day of the week - these are the 3 asterisks) at 2:01AM (the first two fields).

Now because I see you've installed your application under /root I'm assuming you may want to run this app as a system application - which also makes more sense when setting up a server in a VPS anyway. A system crontab is very similar to a user crontab except that is stored in a file under the /etc directory and it has an additional field specifying under which user you want to run it - which is likely going to be root. So the expression might look like this:

1 2 * * * root mono /root/Folder/Aplication.exe

and you probably want to put this in a new file you'd create under /etc/cron.d/ - maybe /etc/cron.d/myapp (note that there is no extension - this is on purpose). Under /etc/ there are several other crontab files and directories, which can be really useful for things like daily runs, such as /etc/cron.daily - read up on them in the crontab files man page I referenced above.

Notes

  1. Don't run Ubuntu 12.04 - its super obsolete. Better try 16.04
  2. Checkout Crontab.Guru for all your crontab expression needs.
  • 1
    Rather than quoting 12.04's obsolescence, I would instead point out that 12.04 support ends in several months – Thomas Ward Jan 8 '17 at 19:09

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