Crontab and C program that should be executed into a terminal window

If the executable file generates an output file, or just modifies some things, the Cron job should be:

* * * * * /home/<user>/Desktop/a.out

If the paths inside the program are relative, their origin point should be properly defined, otherwise the output file will be placed, for an example, into the $HOME directory instead of $HOME/Desktop, where you suppose to be. This is a common mistake.


If the program doesn't write any output file and just generates some data within the stdout, you should redirect it to a file to see it into an appropriate place (this part 2>&1 redirects and the error messages to stdout):

* * * * * /home/<user>/Desktop/a.out >> /home/<user>/Desktop/a.out.log 2>&1

While the stdout isn't redirected, Cron will sending local mails to the user, unless this is overridden by setting the variable MAILTO in crontab:

MAILTO="[email protected]"
* * * * * /home/<user>/Desktop/a.out
  • References: How to detect error in cron jobs | How do I make cron create cron.log?

To execute a GUI application via Cron is more difficult task, because Cron is not designed to work with GUI. Few environment variables should be exported and the best way to do that is to create an additional startup script. Here is an example how to do that within Ubuntu Unity/Gnome:

  • Crate the script. Let's assume it is also located into the ~/Desktop directory and is called a-cron.sh. Do nоt forget to make it executable - chmod +x a-cron.sh. The script should look as this:

    #!/bin/sh
    
    # Set the necessary environment variables - UBUNTU/UNITY/GNOME:
    export DISPLAY=$(w $(id -un) | awk 'NF > 7 && $2 ~ /tty[0-9]+/ {print $3; exit}')
    export GNOME_DESKTOP_SESSION_ID=true
    export DBUS_SESSION_BUS_ADDRESS=$(sed -zne 's/^DBUS_SESSION_BUS_ADDRESS=//p' /proc/`pgrep gnome-session -U $(id -u)`/environ)
    
    #Open new terminal and execute the script:
    /usr/bin/gnome-terminal -x sh -c '/home/<user>/Desktop/a.out; exec bash'
    

    The part exec bash intends to keep the terminal window open after the execution of a.out - there are also other available approaches, that are described here.

  • Now create user's Cron job - crontab -e - as this:

    * * * * * /home/<user>/Desktop/a-cron.sh > /home/<user>/Desktop/a-cron.log 2>&1</pre>
    

    Unlike as the above section, the log file plays different role here. The error messages from the execution of a-cron.sh shall be written inside a-cron.log - so if everything is okay it must be empty.

  • Save and close crontab to apply the job. Wait about a minute to see the result.

References:

  • This answer is the main source of the third section, there could be found also the necessary envvars that shall be exported for desktop environments different from Unity/Gnome.

  • In this question OP uses root's Crontab, but this is not a good idea, because within this approach the ownership of some files and directories inside /run/user/<uid>/dconf/ will be changed, thus a lot of errors will be generated.

  • Cron cannot run gnome-terminal

  • How to open one (or more) gnome-terminal window with few tabs, each with different profile, when start/reboot the computer?

  • Open gnome terminal programmatically and execute commands after bashrc was executed

  • How to programmatically find the current value of DISPLAY when DISPLAY is unset? (for use in crontab) | How to get the display number I was assigned by X

Demo - I just created a simple C program (this is my first attempt) to check how all this works:

enter image description here