Working script to start Chromium not working via cron - cron

I have a Raspberry Pi 3 setup as a kiosk browser. I launch chromium on startup, and now I want to relaunch it a few times a day because chromium sometimes crashes.
I run Raspbian Jessie 8.0
I've made an executable script in etc/cron.hourly/. For testing purposes I currently trigger it every minute using crontab in /var/spool/cron. This is the script:
#!/bin/bash
pkill chromium
export DISPLAY=:0.0
chromium-browser http://www.google.com
If I run this script manually, it works fine. Chromium get killed and the new window opens. (Even from SSH, this works) But when it's triggered by crontab chromium closes, but it doesn't open a new window.
I don't know what to search for, is it a problem of chromium? cron? the script? If have no clue...

It seems that the script does not have a display when run from cron, so chromium's interface does not show.
Add
export DISPLAY=:0.0
to your script before starting chromium and it should work.

chrome uses some environment variables
you can store environment variables from /home/user1/.bashrc file
typeset -gx > /tmp/user1envvars
and /etc/crontab entry would look like
* * * * * user1 /bin/bash -c 'source /tmp/user1envvars ; /snap/bin/chromium args..'

Related

Is there a way to make crontab run a gnu screen session?

I have a discord bot running on a raspberry pi that i need to restart every day, I'm trying to do this through crontab but with no luck.
It manages to kill the active screen processes but never starts an instance, not that I can see with "screen -ls" and I can tell that it doesn't create one that I can't see as the bot itself does not come online.
Here is the script:
#!/bin/bash
sudo pkill screen
sleep 2
screen -dmS discordBot
sleep 2
screen -S "discordBot" -X stuff "node discordBot/NEWNEWNEWN\n"
Here is also the crontab:
0 0 * * * /bin/bash /home/admin/discordBot/script.sh
Is it possible to have crontab run a screen session? And if so how?
Previously I tried putting the screen command stright into cron but now I have it in a bash script instead.
If I run the script in the terminal it works perfectly, it’s just cron where it fails. Also replacing "screen" with the full path "/usr/bin/screen" does not change anything.
So the best way of doing it, without investigating the underlying problem would be to create a systemd service and putting a restart command into cron.
 
/etc/systemd/system/mydiscordbot.service:
[Unit]
Description=A very simple discord bot
[Service]
Type=simple
ExecStart=node /path/to/my/discordBot.js
[Install]
WantedBy=multi-user.target
Now you can run your bot with systemctl start mydiscordbot and can view your logs with journalctl --follow -u mydiscord bot
Now you only need to put
45 2 * * * systemctl restart discordbot
into root's crontab and you should be good to go.
You probably should also write the logs into a logfile, maybe /var/log/mydiscordbot.log, but how and if you do that is up to you.
OLD/WRONG ANSWER:
cron is run with a minimal environment, and depending on your os, /usr/bin/ is probably not in the $PATH var. screen is mostlikely located at /usr/bin/screen, so cron can't run it because it can't find the screen binary. try replacing screen in your script with /usr/bin/screen
But the other question here is: Why does your discord bot need to be restarted every day. I run multiple bots with 100k+ users, and they don't need to be restarted at all. Maybe you should open a new question with the error and some code snippets.
I don't know what os your rpi is running, but I had a similar issue earlier today trying to get vms on a server to launch a terminal and run a python script with crontab. I found a very easily solution to automatically restart it on crashes, and to run something simply in the background. Don't rely on crontab or an init service. If your rpi as an x server running, or anything that can be launched on session start, there is a simple shell solution. Make a bash script like
while :; do
/my/script/to/keep/running/here -foo -bar -baz >> /var/log/myscriptlog 2>&1
done
and then you would start it on .xprofile or some similar startup operation like
/path/to/launcher.sh &
to have it run the background. It will restart automatically everytime it closes if started in something like .xprofile, .xinitrc, or anything ran at startup.
Then maybe make a cronjob to restart the raspberry pi or whatever system is running the script but this script wil restart the service whenever it's closed anyway. Maybe you can put that cronjob on a script but I don't think it would launch the GUI.
Some other things you can do to launch a GUI terminal in my research with cronjob that you can try, though they didn't work for my situation on these custom linux vms, and that I read was a security risk to do this from a cronjob, is you can specify the display.
* * * * * DISPLAY=:0 /your/gui/stuff/here
but you would would need to make sure the user has the right permissions and it's considered an unsafe hack to even do this as far as I have read.
for my issue, I had to launch a terminal that stayed open, and then changed to the directory of a python script and ran the script, so that the local files in directory would be called in the python script. here is a rough example of the "launcher.sh" I called from the startup method this strange linux distro used lol.
#!/bin/sh
while :; do
/usr/bin/urxvt -e /bin/bash -c "cd /path/to/project && python loader.py"
done
Also check this source for process management
https://mywiki.wooledge.org/ProcessManagement

how to put python script into jetson nano startup?

The program should run on boot. the nano operating system (jet-pack) not allowing the auto-login too. I tried to put the script into its startup file but the program doesn't boot.
Run you script in cron job.
sudo crontab -e
* * * /usr/bin/python my_script.py
This will make your script run on boot.
Kindly provide complete paths in script to run error-free

Using Cron tabs to open localhost on a web browser

I am trying to get my PI, which is running rasbian, to automatically run a bash command whenever the pi is booted. The ultimate goal is to open the localhost web page.
I ran
crontab -e
to open my crontab, and added this to the end of the file:
#reboot chromium-browser --kiosk localhost
but when I reboot the pi, nothing appears to happen. I then added this to test to see if crontab was even running or if I had formatting wrong:
#reboot cd && sudo touch sucess
But it executed exactly as expected
What am I missing?
EDIT 1: I did try doing this but it still didn't execute:
#reboot cd && chromium-browser --kiosk localhost
EDIT 2: I also saw an article suggesting that cron cannot find bash command locations, so I tried this as well, also to no avail:
#reboot cd && /bin/chromium-browser --kiosk localhost
EDIT 3: I also attempted to put the same command in root's crontab instead by doing sudo crontab -eBut that didn't work either

Python selenium script does not open a browser on default display from the crontab

I would like to open a web page in a browser on raspberry by running a python selenium script every n hours from the crontab.
Its a pretty standard script that works if I run it locally on that machine.
So if i connect a keyboard to the raspberry and run
python /home/pi/ww/open_dashboard.py >> /home/pi/ww/selenium_output.txt
it will open the browser and do the selenium commands that I need.
Now I can also ssh into the raspberry and run
export DISPLAY=:0 && python /home/pi/ww/open_dashboard.py >> /home/pi/ww/selenium_output.txt
and I can see a browser open and do what I need on the monitor connected to the raspberry.
Now when I add this line to the cron.
57 9 * * * export DISPLAY=:0 && python /home/pi/ww/open_dashboard.py >> /home/pi/ww/selenium_output.txt
I can see that cron executed the command if I check grep CRON /var/log/syslog
Jul 18 09:57:01 raspberrypi CRON[2971]: (pi) CMD (export DISPLAY=:0 && python /home/pi/ww/open_dashboard.py >> /home/pi/ww/selenium_output.txt)
But browser did not open on the monitor connected to the raspberry. What could I be doing wrong here? :)

Raspberry crontab script running

I'm trying to run a command by cron in Raspbian.
If I run ./sec_cam.sh, than my script runs, If I try to run it via crontab every 5 min, than nothing happens.
crontab -e shows me the followings:
*/5 * * * * ./sec_cam.sh
Did I configure the crontab wrong?
Thx in advance
scripts started from a cronjob are not setup with your usual environment, especially not with your current working directory (referenced by the . in ./sec_cam.sh). so to make this work you should specify a full path name like /home/user/sec_cam.sh

Resources