I have set a crontab job like this :

@reboot  xmodmap -e \"keycode 105=Delete\"
@reboot  xmodmap -e \"keycode 66=Home\"

and saved on default directory suggested by NANO (/tmp/..../.../cron), but it's not working. I can list cron job, cron service is running, the output of journalctl -b 0 _SYSTEMD_UNIT=cron.service displays the jobs without errors and so on. Besides that, I also can't run a script at boot: @reboot /home/user/scripts/myscript.sh

Why is cron not working as I expect?

  • Check out some details on xmodmap and how others solve similar problems to your own. – earthmeLon Apr 5 '19 at 7:24
  • @earthmeLon xmodmap is not the problem. crontab is the problem, it's not working. – vladimir pavloski Apr 5 '19 at 9:00
  • why i got -1?? maybe it's a misunderstanding, I have read plenty of tutorials on how to get xmodmap working automatically from boot (start up) and none of them work ! Lets suppose set a script to run at boot time from crontab also it's not working( and it apply to any script on user/home directory or root) , something is missing. – vladimir pavloski Apr 5 '19 at 19:49

xmodmap modifies the keycode mapping for the current session. So if you run it in crontab, it opens a session, modifies the mapping and closes the session again - and its effect is gone.

You should instead run the xmodmap commands in your profile $HOME/.profile (or in your $HOME/.xinitrc if you are starting the X session only at login).

man xmodmap says:

It is usually run from the user's session startup script to configure the keyboard according to personal tastes.

  • the problem is not xmodmap, THE PROBLEM is crontab, I also can not run a script at boot from crontab. I have created an alias in .bashrc to execute xmodmap -e \"keycode 105=Delete\" and it works. Any other suggestions ? – vladimir pavloski Apr 5 '19 at 8:56

Take a look at man cron, or other examples on the Internet. I believe that the problem you are having is you're not using the full path the the binaries.

| melon@pc ~>$ which xmodmap

In your cron -e, instead of xmodmap, you would put /usr/bin/xmodmap.

But, even with this change, you won't see any key changes.

Linux is great in that it provides various solutions to problems, but look into how others have solved similar problems. For example, xmodmap is typically invoked in .xinitrc, which is executed when your X session starts. xmodmap really won't work the way you're trying, at least for your normal user.

Since you're interested in xmodmap, I recommend you look at documentation and examples and forget about cron completely for this current issue.

If you are unwilling to look at documentation and other examples, or want a real understanding of how things work, you can look up the man pages:

XMODMAP(1)                            General Commands Manual                            XMODMAP(1)

       xmodmap - utility for modifying keymaps and pointer button mappings in X

       xmodmap [-options ...] [filename]

       The  xmodmap  program is used to edit and display the keyboard modifier map and keymap table
       that are used by client applications to convert event keycodes into keysyms.  It is  usually
       run  from  the user's session startup script to configure the keyboard according to personal

This last sentence has the answers to your problem.

At this point, you don't know if cron isn't working (this is your suspicion), or if xmodmap fired, but you don't see the effects. The latter is the likely scenario. Your user needs to execute xmodmap within the current X session for xmodmap changes to be effective.

Once you get xmodmap working without cron, you can solve the next problem. As mentioned earlier, you need full system paths, but you also need to tell cron how to run the file. You've simply pointed to the file, which works on your terminal, as it uses the shebang/interpreter (ie: #!/bin/bash). So, for cron to run a bash file...

| melon@nifflheim ~>$ which bash
| melon@nifflheim ~>$ crontab -e
@reboot /usr/bin/bash /home/user/scripts/myscript.sh

Though, do you really want root running a script found in your user directory? With what you've learned so far, maybe you add /usr/bin/bash /home/user/scripts/myscript.sh (or source it) in ~/.xinitrc instead ^_~.

  • nice from you explaining how it should work, now I can try your suggestions, sometimes I got really confused to figure out how I should set xmodmap to work, and for crontab, believe me : it is the first time I try to set a cronjob, I have never done it before. thks. vladi – vladimir pavloski Apr 5 '19 at 20:03

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.