4

I want to execute a script once my computer boots up with cron. But it doesn't work.

My OS is Ubuntu MATE 16.04 LTS.

My crontab enter image description here

My script
enter image description here

My script's absolute path enter image description here

Once my Odroid boots up, the keyboard is still in qwerty.
What am I doing wrong ?

EDIT:
I tried @reboot echo "hi" > /home/valery/reboot.txt 2>&1 It works so the issue is not in the @reboot job.

EDIT: SOLUTION
I run my script from System > Preference > Personal > Startup Applications It's working fine.

Johnrednex
  • 305
  • 5
  • 17

2 Answers2

4

The setxkbmap command depends on the $DISPLAY environment variable to specify the X display it will affect.

On my system, if I run it without setting $DISPLAY, I get:

$ setxkbmap fr
Cannot open display "default display"

Crontab does not set $DISPLAY for the jobs it runs. In fact it provides only a minimal set of environment variables (on my system, just $HOME, $LOGNAME, $PATH, $LANG, $SHELL, and $PWD).

Anything a command prints to stdout or stderr will normally be e-mailed to the owner of the crontab. You're likely to have an e-mail message on your system containing an error message similar to the above.

It's likely that X Windows isn't even running yet when crontab executes the @reboot job. You'll have to find another way to run that command automatically, or just run it manually. (Or there could be another way to do this that I haven't thought of.)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • I ran into a similar problem, and the solution that worked was to put the following into "Startup Applications": `bash -c "xkbcomp -I$HOME/.xkb ~/.xkb/keymap/mykbd $DISPLAY"`. (It didn't work without wrapping it into the `bash -c "..."` and I'm not sure exactly why.) By the way, "Startup Applications" seems to be simply creating a `YOUR_COMMAND_NAME.desktop` file under `~/.config/autostart`. – Evgeni Sergeev Sep 05 '19 at 02:18
  • @EvgeniSergeev: That doesn't use cron -- but of course if it works, it works. My guess is that the `bash -c` was needed so that `$HOME` and `~` could be expanded. (Probably cleaner to use `$HOME` consistently.) – Keith Thompson Sep 05 '19 at 02:55
1

Why not use systemd/upstart job for this?

PS:

Example

Try this example. If it wouldn't work then try to find a bug

Replace /home/valery/ with a path to your home folder:

$ crontab -l
@reboot echo "hi" > /home/valery/reboot.txt 2>&1

I then rebooted the system.

$ sudo reboot

After the reboot.

$ cat reboot.txt 
hi

Bugs

  1. cron: @reboot jobs are not run.
  2. The bug in Ubuntu would seem to be confirmed here in this SO Q&A titled: @reboot cronjob not executing.

Someone was attempting the very same thing and getting frustrated that it didn't work. It's titled: Thread: Cron - @reboot jobs not working.

Community
  • 1
  • 1
Valeriy Solovyov
  • 5,384
  • 3
  • 27
  • 45