Cron job to open a tab in a browser

johnlinux

Member
Joined
Jan 2, 2024
Messages
35
Reaction score
2
Credits
304
I have Debian 12 and created a script to open a tab in a browser for a specific web site. The script works when I run it locally './script.sh" but when I use Cron to execute the script a message saying "Firefox is already running" so Cron is trying to open Firefox.

The log say
ATTENTION: default value of option mesa_glthread overridden by environment.


What I do not understand is everything seems to point to a environment issue but since Cron is run under my accout what could cause a difference between cron and me running the script ?
 


I am running the cron as a user and The script itself has no problems running manuallyand the crontab is tryig to run the script. I did inlcude in the script.

export DISPLAY=:0:

Why is there a differnce between me as a user and crontab as a user running the same script
 
hi @johnlinux
it is the crontab program that needs export DISPLAY=:0: to start gui program.
you need to find your bash. whereis bash
the line below added to your user crontab , will open-web.sh: at 2:30 pm
30 14 * * * /usr/bin/bash /home/flicker/open-web.sh >/dev/null 2>&1
 
I have Debian 12 and created a script to open a tab in a browser for a specific web site. The script works when I run it locally './script.sh" but when I use Cron to execute the script a message saying "Firefox is already running" so Cron is trying to open Firefox.

The log say
ATTENTION: default value of option mesa_glthread overridden by environment.


What I do not understand is everything seems to point to a environment issue but since Cron is run under my accout what could cause a difference between cron and me running the script ?
There could be a few reasons cron isn't running the script.

What does cron think the PATH is? If this is the issue, one can specify the PATH in the cron entry with something like this:
Code:
PATH=/usr/bin:/bin:/usr/sbin:/home/bin/<user>
 36 0 0 * * * <user> <command/script>
where the command is known to be in the path.

Alternatively, specify the command/script with an absolute pathname, e.g.
/home/<user>/bin/<scriptname>

If neither of those suggestions are helpful, you could invoke the script through the shell, e.g.
bash -c <absolute_pathname_of_script>
 
Why is there a differnce between me as a user and crontab as a user running the same script

Cron doesn't open an Xwindows session by default, you probably do.

I just tested this with firefox I already had firefox open when I ran this.

Code:
#!/bin/bash
firefox --new-tab "https://www.linux.org"

It works from cron, but only if I'm currently logged into an Xwindows session.

This also seems to work.

Code:
#!/bin/bash
google-chrome --new-tab "https://www.google.com"

But again, only if I'm actually logged into an Xwindows session.

I can lock the screen, and it still seems to work. But I have to be logged in.
The new page will be up when I unlock the screen.

But if I never log in, or log all the way out, it doesn't have an Xwindow session
to create the browser in.
 
Last edited:
There could be a few reasons cron isn't running the script.

What does cron think the /home/macneal/Documents/scripts/satattwelve.shPATH is? If this is the issue, one can specify the PATH in the cron entry with something like this:
Code:
PATH=/usr/bin:/bin:/usr/sbin:/home/bin/<user>
 36 0 0 * * * <user> <command/script>
where the command is known to be in the path.

Alternatively, specify the command/script with an absolute pathname, e.g.
/home/<user>/bin/<scriptname>

If neither of those suggestions are helpful, you could invoke the script through the shell, e.g.
bash -c <absolute_pathname_of_script>
I have tried the absolute path and the bash -c with the absolute path and I get the message "firefox is alreadyrunn, but not responding." Yet running the script directly works.
 
Cron doesn't open an Xwindows session by default, you probably do.

I just tested this with firefox I already had firefox open when I ran this.

Code:
#!/bin/bash
firefox --new-tab "https://www.linux.org"

It works from cron, but only if I'm currently logged into an Xwindows session.

This also seems to work.

Code:
#!/bin/bash
google-chrome --new-tab "https://www.google.com"

But again, only if I'm actually logged into an Xwindows session.

I can lock the screen, and it still seems to work. But I have to be logged in.
The new page will be up when I unlock the screen.

But if I never log in, or log all the way out, it doesn't have an Xwindow session
to create the browser in.
I am running Debian 12 and I am currently logged into the Xwindows session. The script run and opens the site but the cront tab script
00 20 * * * /home/username/Documents/scripts/scriptname.sh
produces "firefox is already running" message
 
I am running Debian 12 and I am currently logged into the Xwindows session. The script run and opens the site but the cront tab script
00 20 * * * /home/username/Documents/scripts/scriptname.sh
produces "firefox is already running" message
I guess if @dos2unix in post #6 has been able to run the cron, but your script is failing, perhaps you could amend your script bearing in mind what he has shown. Please feel free to paste the script here ... it may help readers see what the issues are.
 
I guess if @dos2unix in post #6 has been able to run the cron, but your script is failing, perhaps you could amend your script bearing in mind what he has shown. Please feel free to paste the script here ... it may help readers see what the issues are.
Incorrect. the script runs exactly as I need. Only when running it with Crontab it fails
 
. Only when running it with Crontab it fails

What are the errors in /var/log/cron or /var/log/cron.log
if you use the --new-tab you won't get the "browser is already open" error.
Are you logged in when the cron fails? You might also check dmesg.
 
Incorrect. the script runs exactly as I need. Only when running it with Crontab it fails
Just to correct any misunderstanding, with the expression in post #9 "your script is failing", the reference to its failure to operate was as a cron entry, not as a failure to achieve the opening of the tab. Sorry for the lack of clarity there.

The scripts shown in post #6 were said to work, so it seemed like a good idea to try those, hence my suggestion.
 
Last edited:

Members online


Top