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

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? :)

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

Why my bot.py code using selenoid isn't running on ubuntu server when I close my ssh bash window?

I have set up on local machine an ubuntu server on which I run bot.py code using ssh bash terminal. My bot.py get's url from my contacts and visits webpages using docker and selenoid. I have set up docker and selenoid and they work well. When I run:
$ sudo ./myscript_ro_run_bot.sh
[inside myscript_ro_run_bot.sh]:
#!/bin/bash
while true
do
echo "running bot.py"
nohup sudo python3 bot.py # nohup to run at background
wait
echo "bot.py finished"
echo "running bot1.py"
nohup sudo python3 bot1.py
wait
echo "bot1.py finished"
.....
echo "runnning bot5.py"
nohup sudo python3 bot5.py
sleep 10m
done
(I have 5 bot.py files)
I can see on local machine messages in Telegram that (myscript_ro_run_bot.sh) doing it's job well that sites have been visited and I get rewarded. Even on local machine the (myscript_ro_run_bot.sh) can be ran 24/7 hours (indefinitely). But I want to run on server 24/7 hours. The problem is when I close ssh bash window manager I see on local machine Telegram that nothing happening, I don't get messages. Here is the trick when I connect to my server with ssh again after 5 or an hour and only after reconnection I start receiving messages in telegram. I can see job running in server with command:
$ htop
that my command sudo python3 bot.py is running
When I used:
$ sudo crontab -e
#reboot /home/user/myscript_ro_run_bot.sh >> /home/user/myscrit_to_run_bot.log
After reboot I connected to server with ssh and got result from myscrit_to_run_bot.log:
running bot.py
bot.py finished
running bot1.py
bot1.py finished
running bot3.py
But I didn't get any messages in telegram after reconnection.
Whereas I run my script manually and reconnect to server I get messages in telegram.
Can anybody help me how to solve the issue? I want sudo ./myscript_ro_run_bot.sh to run even I close ssh bash terminal.
If you want me to provide more details please write commands as well (detail instruction) because I am new on coding and linux.
I appreciate your help
Try to use screen or tmux for launching your process https://wiki.archlinux.org/title/Tmux
run tmux
run ./your_program
enter ctrl+b and then d
After this, your process will be run in the background, and you can close ssh-connection.
When you need back, you can run tmux attach

Working script to start Chromium not working via 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..'

No cursor motion capability occurs when startup linux script execute minicom script with at commands

I have created a startup Linux script for a board that runs Debian jessie Armbian 8.1. I need at startup to run at command in a modem attached to it via USB interface. I've put the script inside rc.local:
#!/bin/sh -e
cd /home/user/Script/
sh script.sh
exit 0
In the folder Script I have a script:
#!/bin/sh
minicom -S at_command.txt
The at_command contains:
send at+creg=1
send exit
! killall minicom
The issue is that when I run the script manually it works correctly,
however when I start the the board the at commands do not execute.
Since the board is Debian I also followed the link: ttp://www.stuffaboutcode.com/2012/06/raspberry-pi-run-program-at-start-up.html and to adapt the script to run according LSBInitScripts but I got the same behavior. When running sudo /etc/init.d/myScript start manually everything works great, but after Linux startup no at_command is executed. I added in the rc.local the following commands for debugging:
set -x;
exec 2>>/home/user/Script/minicom-rc.log;
date +"$0 %c" >&2
And in the generated log file I got this printout:
+ date +/etc/rc.local %c
/etc/rc.local Fri 02 Mar 2018 04:02:43 PM EET
+ cd /home/user/Script/
+ sh script.sh
No cursor motion capability (cm)
What I have done wrong?
I am guessing it is related to there being no terminal during startup.
Maybe try adding the following line before starting minicom:
export TERM=linux-c-nc
In this scenario you alternatively use my atinout program which is a program specifically made for sending AT commands to a modem from the command line (assuming the modem is /dev/ttyUSB0):
echo at+creg=1 | atinout - /dev/ttyUSB0 -
or possibly
atinout /home/user/at_commands.txt /dev/ttyUSB0 /dev/null

Using SSH to open application on desktop

So normally people ask how to forward x11 to the local machine, but rather I want to leave the application running on the remote box's desktop.
So let say I ssh from a windows machine (using putty) and run xclock & disown. If I then walked over to my desktop and look at the screen and see xclock running on the linux machine.
Any way to do that? Using Mint 13.
This works for me once I'm ssh'd in:
export DISPLAY=:0; nohup <command> &>/dev/null &
For example:
export DISPLAY=:0; nohup iceweasel &>/dev/null &
When you're ssh'd in normally, set the DISPLAY variable with export DISPLAY=0:0 (0:0 being the display of the target box), and then you can run your command as you normally would. If you want to be able to close the ssh session, prepend the command with nohup: nohup ./yourcommand > dev/null.
h4bo --> That worked. just had to make the script.
First part of learning ssh stuff. Now instead of leaving teamviewer (a remote desktop program) open all the time, I can launch when I need it and then use it.
i guess
nohup ssh -X <ip address> <application name>
I recently had to do some python tkinter development on a Raspberry Pi with Thonny, where I wanted the tkinter graphics to show on the Pi's X server. For this specific purpose, I created a /usr/bin/pythonx script which simply consisted of:
#!/bin/bash
DISPLAY=:0 python "$#"
This wrapping technique works when Thonny ssh's into the Pi where the user is also logged in at the GUI desktop, as the ssh user has access to the .Xauthority data needed to send X client requests (application) to the server (GUI).

Resources