'Crontab not working, but works manually for python call
That's my first ask on StackOverflow, wow !
I have to build a program for a project about network, for my last studying year in France. The project itself is (almost) written in Java, except for a function that require Python, in order to take a screenshot in a web browser using selenium (yes, I tried using selenium in Java, unsuccessfully).
What's more, this Java program is called by a previous python project : this project has to run on a Raspberry Pi, with a button to start the scan.
And in order to start this Raspberry pi's management program (in python), it has to be launched by a .sh file, called at launch by a crontab command.
The problem is that, after debugging, I see that when the .sh is launched at the boot of the RPi, the call to the python function for screenshot isn't working, whereas it does without any problem when I launch the .sh manually.
Here is a draft of the final idea:
- (Boot Raspberry)
- crontab
- .sh
- python program (n°1, Raspberry's management)
- (if button) Java program (checking network)
- (if capture needed) python program (n°2, web browser screenshot)
Then the step n°6 is working if the .sh file is launched again, but manually, and isn't working at the 1st boot of RPi, when .sh is launch automatically
It may be quite hard to understand, so do not hesitate if you have any question!
Thanks :)
EDIT (thanks for your replies)
In crontab, I wrote (after a sudo crontab -e):
@reboot sh /home/pi/launcher.sh >/home/pi/logs/cronlog 2>&1
In this laucnehr.sh, we go to the directory where are all programs, including the one for the management. Everything work fine manually, but the call from Java to the Python script to screenshot a specific website.
Call From Java, to the Python program that works only manually:
public synchronized void http_httpsRequest(String protocole){
try {
String command = "python3 /home/pi/Documents/FINAL_ScanMultiThread/programs/screen.py " + protocole + " " + ip;
Process process = Runtime.getRuntime().exec(command);
process.waitFor();
} catch (IOException e){
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
}
Python program to screenshot:
from selenium import webdriver
import sys
import os
from time import sleep
def main():
protocol = sys.argv[1]
address = sys.argv[2]
path ="../screenshotsWEB"
if not os.path.exists(path):
os.makedirs(path)
options = webdriver.ChromeOptions()
options.add_argument("-headless")
options.add_argument("-disable-gpu")
options.add_argument("--ignore-ssl-errors=yes")
options.add_argument("--ignore-certificate-errors")
chrome = webdriver.Chrome(options = options)
chrome.get(protocol + "://"+ address)
name = path + "/" + address+ "_" + protocol + ".png"
sleep(10)
chrome.save_screenshot(name)
chrome.close()
if __name__ == "__main__":
main()
FIX
There was an error in the last python script, that wasn't showing up (I don't know why ?).
So after launching this script automatically from the .sh file, errors where showing up, it was a problem from the "webbrowser", so I added 2 options, that are:
options.add_argument("--no-sandbox")
options.add_argument("--windows-size=1420,1080")
Thanks y'all :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
