1

I have gone through many answers as to how to add a crontab through terminal through one liners and came across only one single option everywhere which is

{crontab -l; echo "1 * * * * /usr/bin/firefox" } | crontab -

Running which all I am receiving is

>

That's it. A promt for me to type something.

and second option being

(crontab -l; echo "1 * * * * /usr/bin/firefox" ) | crontab -

Which seems to add the cron to /var/spool/cron/crontabs/root but does not open firefox every minute, in fact it does not open at all.

I read most on most answers that you should not edit the /var/spool/cron/crontabs/root or /etc/crontab files directly.

Is this not supported in my system or what?

An output of uname -a gave the following description of my system

Linux earth 4.9.0-kali4-amd64 #1 SMP Debian 4.9.30-2kali1 (2017-06-22) x86_64 GNU/Linux

EDIT: Following message logs are repeated often in my /var/spool/mail/mail logs

From root@localhost.localdomain Sun Jul 09 16:01:12 2017 
Return-path: < root@localhost.localdomain > 
Envelope-to: root@localhost.localdomain 
Delivery-date: Sun, 09 Jul 2017 16:01:12 +0530 
Received: from root by earth with local (Exim 4.89)   
    (envelope-from <root@localhost.localdomain>)  
    id 1dU9UY-0001Ry-3A   
    for root@localhost.localdomain; Sun, 09 Jul 2017 16:01:06 +0530 
From: root@localhost.localdomain (Cron Daemon) 
To: root@localhost.localdomain 
Subject: Cron <root@earth> /usr/bin/firefox 
MIME-Version: 1.0 
Content-Type: text/plain; charset=UTF-8 
Content-Transfer-Encoding: 8bit X-Cron-Env: < SHELL=/bin/sh > 
X-Cron-Env: < HOME=/root > 
X-Cron-Env: < PATH=/usr/bin:/bin > 
X-Cron-Env: < LOGNAME=root > Message-Id: < E1dU9UY-0001Ry-3A@earth > 
Date: Sun, 09 Jul 2017 16:01:06 +0530

Error: GDK_BACKEND does not match available displays

2 Answers2

2

Most likely your second attempt is correct, but your expectation is wrong.

Let's look at it in parts:

crontab -l

lists all existing entries for the current user's crontab. The

echo "1 * * * * /usr/bin/firefox"

just prints that line again. These two commands are then grouped together in a subshell and the common output is piped into

crontab -

So the crontab is overwritten by what comes in via the standard input, which in this case is the old crontab plus the new entry.

As you said it is added to the crontab file. And, assuming the cron daemon is running, the command will be executed each minute.

So why aren't you seeing a firefox window each minute? - Because the conrjob runs in a different shell below the cron daemon, which doesn't have access to your X session, thus firefox will fail and report something like

(firefox:22376): Gtk-WARNING **: Locale not supported by C library.
    Using the fallback 'C' locale.
Error: GDK_BACKEND does not match available displays

And terminate. How to see that error? Typically the cron daemon will try to send you a mail, see /var/spool/mail eventually.


About the two forms:

{ crontab -l; echo "1 * * * * /usr/bin/firefox" } | crontab -

would have to be written as

{ crontab -l; echo "1 * * * * /usr/bin/firefox"; } | crontab -

(mind the extra semicolon)

The difference between () and {} is that the former creates a sub-shell, whereas the later executes the commands in the same shell context. Thus variable assignments survive in one form, not in the other.

johannes
  • 271
  • I don't know, it adds 1 * * * * /usr/bin/firefox to /var/spool/cron/crontab/root but it just doesn't start firefox. Had a look on service cron status. it is running too. It does not add this to /etc/crontab although, if that is important. – GypsyCosmonaut Jul 08 '17 at 22:05
  • I'm quite sure it starts firefox, but firefox doesn't find a display it can use. Check your logs and mail (i.e. in /var/spool/mail) – johannes Jul 09 '17 at 11:05
  • yes there is an error Error: GDK_BACKEND does not match available displays repeated so many times in my logs, check the question above, I have updated it with the error. What do I do? – GypsyCosmonaut Jul 09 '17 at 11:32
  • What's your actual goal? Adding cron entries works. – johannes Jul 09 '17 at 11:57
  • I have my large data uploaded to mega.nz website which requires users to open its website at least once in 2 months or else they'd assume the account inactive and delete the data. So, I want Firefox to open mega.nz once every 2 weeks (in order to be on safe side). So can cron have access to X by any chance? If not, how can I achieve this? – GypsyCosmonaut Jul 09 '17 at 14:50
  • This sounds like a job for curl or wget or maybe some short scripting ... but too broad for this site – johannes Jul 09 '17 at 16:40
  • Hi again @johannes , I found the solution, so thought it might be helpful to you and others to mention here. Yes you were right, the cron is running but cron does not have access to X sessions. I simply added this line to my cron and it worked. 1 1 * * * export DISPLAY=:0 && xhost local:$USER && /usr/bin/firefox so now it works perfect. Found my solution from this link with your help. https://serverfault.com/questions/280558/need-with-crontab-and-gui-python-popup Thank you so much.. – GypsyCosmonaut Jul 11 '17 at 07:39
0

I was getting the same error when trying to have firefox-esr launched from a cron job.

Setting DISPLAY right inside the cron file has helped though:

* * * * * pi export DISPLAY=:0 && /usr/bin/firefox-esr &

(Make sure to figure out your actual DISPLAY number by doing echo $DISPLAY)