How to make "pyscreenshot grab" work in service file - python-3.x

I am a freelancer and my clients need screenshots of all the work I did. So I made a python script which takes screenshots every 7 minutes and this code gets executed automatically when system starts. It worked perfectly in Ubuntu. This is the code.
Now I switched to Fedora34. I have modified my script litte bit which captures screenshots and saves in folders according to date rather than putting all screenshots in one directory. This is my script for Fedora
#!/usr/bin/env python
import pyscreenshot
import time
from datetime import datetime
import os
from pathlib import Path
while True:
# Get Current time
now = datetime.now()
# setting current datetime as image_filename
image_filename = now.strftime("%b-%d-%Y|%H:%M:%S")
# Getting month, year and date
today_month_year = now.strftime("%b-%Y")
today_date = now.strftime("%d")
new_directory = os.path.join(r"ss/",
str(today_month_year),
str(today_date))
# Create folders safely
Path(new_directory).mkdir(parents=True, exist_ok=True)
# To capture the screen
image = pyscreenshot.grab()
# To save the screenshot
image.save(new_directory + "/" + str(image_filename) + ".png")
# Each screenshot Every 7 minutes
time.sleep(420)
And fedora does not have Startup Applications app and gnome-tweaks > Startup Applications does not support custom scripts. So I searched at many sites and came to conclusion that I can create a service file. And I created a service file to start my script at startup with following code:
File: /etc/systemd/system/screenshots.service
[Service]
WorkingDirectory=/home/<uname>/path
ExecStart=/bin/sh -c 'cd /home/<uname>/path/ && source env/bin/activate && ./auto-ss.py'
Now when I run sudo systemctl start screenshots, it does not throw any error but running sudo systemctl status screenshots throws following error:
pyscreenshot.err.FailedBackendError: All backends failed!
According to this answer
PyScreenshot is for taking screenshots of desktop applications. Since your application is running on a server, there's nothing to take a screenshot of. If you intended to take a screenshot of the user's browser, you would use frontend tooling, which runs in the browser, not a server-side tool.
If this is the problem, how can I run my python app as frontend. And if its some other problem, how can I overcome it?
Can anyone help me. I am stuck on this since very long :( . Well, thanks in advance :)

If it works on Ubuntu and doesn't work for Fedora, the sure way to fix it is to mimic Ubuntu on Fedora (i.e. via desktop environments). Ubuntu uses gnome. So, your best bet is to use gnome on Fedora too.
Please have a look at these links:
What is the recommended way to install Gnome on Fedora 30 Server?
Install Gnome 3 in Fedora 22 KDE Spin
Fedora 32: Gnome Desktop

Related

Run scripts on start up on Google Cloud Platform (GCP) does not work for some scripts?

I have a Linux machine on GCP and followed this answer to add a startup script. When my file is the following it perfectly works and creates a log file when the machine starts:
#!/usr/bin/python3
with open('./my_log.txt', 'w') as f:
f.write('Hello Saeed\n')
f.close()
However, when I change it to any other files like:
#!/usr/bin/python3
from package import *
import numpy as np
import pandas as pd
I do not see the run file on my python process when the machine starts. I use ps -fA | grep python to see my python processes.
Can you please help me to figure this out?
Edit:
I import some packages it does not work.
I would recommend you check out the "official" way to install and run a startup script on a GCE VM: https://cloud.google.com/compute/docs/instances/startup-scripts. Using a cron job might work, but it's sort of a "low-level" solution given that GCP provides more managed and theoretically more straightforward ways to provide a script that should be executed every time your VM starts up.
As per your script, it might be a couple of things: either it finishes executing before you have a chance to run ps -fA | grep python, or it may be failing for some other reasons, probably missing dependencies. You can try the following test: run your script as root with sudo ./your_script.py. You should be able to see some errors and then you can start troubleshooting those to find a solution to your problem. The reason you need to run the script as root is that startup scripts are run as root, and thus such a test will represent what will happen at startup time more closely.
If you see missing dependencies problems you should be able to solve them by installing the python dependencies (like numpy) at the system level using a command like sudo pip install numpy. If you don't want your script to run as root you can also add the username that you want your script to be run as after the command in the crontab:
#reboot /path/to/script username
Hope this gets you on the right path :)

PyVisa and Agilent 823578 on Mac

I am trying to get PyVisa working on my mac. I have installed it and NI libraries but when I try to run the following simple python file from terminal in VS code:
import sys
import pyvisa as visa
rm = visa.ResourceManager()
rm.list_resources()
inst = rm.open_resources('GPIB::10::INSTR') #Instrument GPIB Addr = 10
print (inst.query("*IDN?"))
By running 'python3 temp.py' I get the following error message:
Error Domain=KMErrorDomain Code=71 "Insufficient permissions for action: Unable to load 'com.highpoint-tech.kext.HighPointRR' in unprivileged request." UserInfo={NSLocalizedDescription=Insufficient permissions for action: Unable to load 'com.highpoint-tech.kext.HighPointRR' in unprivileged request.}
zsh: abort python3 temp.py
Make sure com.ni.driver.NiViPciK extension is installed and loaded. For this, go to About This Mac -> System Report -> Extensions (in Software section) and find NiViPciK. It will likely say "Loaded: No". You need to manually allow National Instruments extensions.
Boot into recovery mode by holding Cmd-R while powering up.
Open Terminal from Tools menu.
Execute: spctl kext-consent add SKTKK2QZ74
Reboot
This did the trick for me:
I have been using NI IVI backend, but lately gave a spin to open source PyVISA-py backend instead and it has worked great for our automation purposes both on OSX and Linux.
I changed
rm = visa.ResourceManager() to rm = visa.ResourceManager('#py').
Some minor modifications may be needed (I had to remove instrument.clear() calls for some reason).

Script that opens cmd from spyder

I am working on a text adventure with python and the issue i am having is getting spyder to open a interactive cmd window. so far i have tried os.systems('cmd / k') to try and open this which it did but i could not get any code to run and kept getting an app could not run this file error. my current code runs off a import module that pulls the actual adventure from another source code file. how can i make it to where only one file runs and opens the cmd window to play the text adventure?
(Spyder maintainer here) Cmd windows are hidden by default because there are some packages that open lot of them while running code (e.g. pyomo).
To change this behavior, you need to go to
Tools > Preferences > IPython console > Advanced settings > Windows adjustments
and deactivate the option called Hide command line output windows generated by the subprocess module.
I found myself in a similar situation, developing code for a research group with heavy Spyder usage. I came up with this workaround to force usage of the vanilla Popen class regardless of whether or not it is run from Spyder (full disclosure I have not tested it very aggressively):
import subprocess
try:
from spydercustomize import SubprocessPopen
except ImportError:
SubprocessPopen = subprocess.Popen
else:
SubprocessPopen = SubprocessPopen.__bases__[0]
process = SubprocessPopen()

Trigger webpage view from linux command line

I am using a shared file download service which deletes the file if the file's web page has not been visited in 30 days. I need to store my files for longer than 30 days.
I am using a Centos 7 based Linux server.
My idea was to create a cron to run a bash script with a command line for each file.
I have tried using the wget, curl and lynx commands but these do not register as the page being visited.
Any ideas on a command that I can use?
The file sharing service is gofile.io and an example file I have uploaded (on the page that is required to be visited) is https://gofile.io/?c=znRpuJ
Edit: After looking into it further there appears to be a javascript function which needs to be activated which I think isn't activated by wget/curl/lynx. Is there any way to activate this javascript from the command line?
Try this:
Make sure you have Python3.
Install geckodriver.
pip install selenium for Python3.
Run the following script:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://gofile.io/?c=znRpuJ"
driver.quit()
Good luck.

Megasync does not autostart

I installed the ubuntu(14.04 VirtualBox) version of megasync along with the nautilus extension. I typed "megasync" in my console, logged into my account, enabled autostart. But upon closing the console megasync just closes and when I restart the OS megasync does not start itself up.
Megasync is included in the startup options with command "megasync".
Note, I just installed Ubuntu and have no previous experience with Linux so this might be just a misunderstanding from my side, but I am helpless right now.
I also had problems with autostart. In my case it was a prolem with permissions (megasync was not marked as executale), but I don't think yours is the same.
You can give it a try: sudo chmod +x /usr/bin/megasync
Or just run a script I've created to run multiple instances.
1) Log out from the session
2) Download and run my script MEGA-Instances and follow the guide.

Resources