2

I've got a script that works perfectly when I run it as myself on the command line but when the script is run by incron the line that copies a value to the clipboard doesn't work. Basically I'm using incron to monitor the addition of a new file in a particular directory then upload that file to an FTP server and copy the URL to the clipboard. This is the line that fails (seemingly does nothing) when the script is run by incron:

echo -n $URL | xclip -selection clipboard

My guess is that incron doesn't have access to the clipboard in the same way that I do as a logged in user, but I don't know what to do about it. Any help would be awesome.

  • 1
    The most common problem with cron is that cron runs with a minimal shell and minimal environmental variables. As such you have to use the full path to binaries (/usr/bin/xclip for example). – Panther Jun 10 '15 at 3:59
  • Is there a specific reason to do it (whatever it is) via the clipboard? Why not let a script handle whatever is the follow up? – Jacob Vlijm Jun 10 '15 at 7:02
  • I wanted to monitor a folder so I could drop a new file in the folder and the file would be automatically uploaded to my website with the URL to the file in the clipboard so I could easily copy/paste the URL into a chat or email or whatever. Thanks so much for the help! – Lee Jun 10 '15 at 19:37
2

xclip is an application that needs environment variables such as $XAUTHORITY and $DISPLAY to talk to the X11-server. You can create a cronjob the looks as follows:

/bin/su your_username -c "export XAUTHORITY='/home/your_username/.Xauthority'; export DISPLAY='$(strings /proc/$(pgrep -n Xorg)/environ | awk -F== '$1 ~ "DISPLAY"{print $2}')'; echo -n "message" | xclip -selection clipboard"

Use your own username instead of your_username. It is a long line, but cron doesn't accept multiple lines as cronjob entries. However, here is what it does:

  • /bin/su your_username: it switches the user
  • -c "...": and runs the command in the quotes
  • export XAUTHORITY='...';: first we need the $XAUTHORITY variable
  • export DISPLAY='...': and the $DISPLAY variable, this is mostly :0, but if not, we can get the variable from the environment of the Xorg process
  • echo ... | xclip ...: finally run the desired command within that environment. No need to use absolute paths anymore because we have a minimal environment now.
  • This is excellent information! Thank you! I put this line at the top of the screen and it started working. export DISPLAY=:0.0 – Lee Jun 10 '15 at 19:35
  • 1
    @Lee Then accepting the answer is the appropriate way to indicate it works for you :) – Jacob Vlijm Jun 10 '15 at 19:39
  • Doh! Sorry about that. This was my first question I've ever asked. #embarrassed – Lee Jun 10 '15 at 23:36
  • Didn't work for me, so I put my script in sudo crontab -e (root crontab) and had it only the following content: export XAUTHORITY='/home/username/.Xauthority'; export DISPLAY=:0.0; command, xclip, program etc. Reason: the xorg thingy doesnt work for me, so I replaced it with my actual $DISPLAY variable and su wouldnt work in a user-crontab, so I got rid of it and ran it as a root crontab in the first place. thanks for your answer – Blauhirn Nov 28 '16 at 1:38

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.