0
matthew@matthew-pc:~$ crontab -l 
[...]
@reboot 1 5 * sleep 10s && export DISPLAY=:0 && xdg-open ~/@/Testing.jpg

"Testing.jpg" was not opened at Linux startup on May 1.
How can I use Crontab to open a file at Linux startup on a specific date, e.g. May 2?

1

You can't mix a schedule with @reboot - cron doesn't support that.

If you want to open a file at Linux startup on a specific date, do this:

  1. Write a script (chkdate.sh) that checks the date for a match to (for example) May 2

  2. Use the @reboot spec in cron to run chkdate.sh

[EDIT: re chkdate.sh, a script to check today's date against a specific date]:

You could write a bash script to accomplish this. The date function in bash will return today's date. Having that, you only need then to test today's date against the specific date, and take the desired action. Here's one way to approach this:

NOTE: I use a Debian-based Linux distribution, so what follows reflects that. Your system may vary somewhat.

  1. Use the date function: Open a "terminal window", or simply get the command prompt on your system. At the command prompt ($ in my case), type date +%Y-%m-%d:
$ date +%Y-%m-%d
2019-05-02
$
  1. Specify the date format needed: date can provide its output in a variety of different formats. In the example above, the format is "Year-month-day". If you are interested only in the month and day, then the command would be: date +%m-%d:
$ date +%m-%d
05-02
$
  1. Regardless of the format you need, you must learn to use the system documentation. To learn how to instruct date to provide its output in the format you need, you will need to read its documentation:
$ man date
...
q
$

Note that the single character q will close the man page.

  1. Once you've decided on the format of the date output, you can begin to write the script chkdate.sh. Let's create the file, and open it in an easy-to-use text editor:
$ nano ~/chkdate.sh
...

You may begin entering your script; for example:

#!/bin/bash
SOMEDAY="05-02"
TODAY=`date +%m-%d`
if [ $TODAY == $SOMEDAY ]
then 
   echo "Today is the day!"
else
   echo "Today is NOT the day."
fi

Now, save the file ^o, and exit the nano editor ^x.

  1. You will need to set the file permissions to make the script executable, and you may then run it:
$ chmod 755 ~/chkdate.sh
$ ~/chkdate.sh 
Today is the day!
$ 

Because today is May 2nd, the then condition of the if statement is executed (echo "Today is the day!"). On any other day of the year, the else statement would be executed. Also note that it is not necessary to have an else condition - you may simply omit it if you don't need it.

  1. Assuming this executes, you can modify the script to replace the echo ... command(s) with the command(s) you want to use.
  • I don't know how to write chkdate.sh. Please help. – Matthew Wai May 2 at 3:05
  • @MatthewWai: This is not a script-writing service, but I'll help you. What programming languages do you work in - bash, Python, C...? In bash, you could use the date function; I've edited my answer, and added a bit to get you started. – Seamus May 2 at 11:08
  • Many thanks to Seamus, who has helped me with my second bash script. My first bash script is this. Hopefully, I will soon learn the third one. – Matthew Wai May 2 at 11:50
  • @MatthewWai: This was probably more than you needed. I incorrectly assumed you had less experience than you obviously do. A couple of other points: 1) The at package may provide another solution, and 2) systemd has an advantage over the @reboot facility in cron. – Seamus May 2 at 12:08
  • On my Linux Mint, echo "Today is the day!" brings up nothing, while zenity --info --text="Today is the day!" brings up a message box. – Matthew Wai May 3 at 15:30

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.