How to make ubuntu OS to get sleep using python? - python

using python coding i want to make make the system to get sleep state , is that possible ?
i need the solution in Ubuntu , i can find that for windows but for Ubuntu is their any solution ?

Using os.system you can call terminal commands.
You can for instance use systemctl hibernate to get Ubuntu to sleep.
import os
os.system("systemctl hibernate")
Note: you might need to run your python file using sudo.

Related

Kali Linux Vscode Python - Operation not permitted run python file with internal terminal

Hy guys, i am trying to use vscode in kali linux.
But I ran into a problem.
The moment I start the python file from the play symbol in the terminal it gives me a permission error.
But if I boot from the terminal using sudo python main.py it works fine.
Would you know how to help me fix this problem?
i am used to launch with play, i find it annoying to type in the terminal every time.
Screen error
I also tried the solution found on the internet of sudo code --user-data-dir = "~ / .vscode-root" my/path, but it doesn't solve the problem.
the code to execute:
import socket
import struct
import binascii
s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(0x0800))
while True:
print (s.recvfrom(2048))
Try to use :
sudo code
in the terminal
If it doesn't work try to remove it and install it again.

How to use a python IDE as a substitute for command prompt?

It is possible to open a program on your pc using the command prompt. Is there a way to use a Python IDE or an idea for code that helps me do that?
You can use:
import os
os.startfile("application_name.exe")
Or if you want to run external applications too, you need to check the path for this application.
import os
os.chdir(r'C:\program files\programfolder')
os.startfile("application_name.exe")

Call alias using a Python Script

I'm writing a python script that utilizes Ureka (a distribution of different astronomy packages). In order to run any Ureka's packages, the user must first initialize Ureka by typing "ur_setup" in the terminal. It turns out "ur_setup" is an alias for the following command:
'eval `/Users/rem/.ureka/ur_setup -csh \!*`'
How would I be able to include this in my python script and have it work?
Thanks in advance!
Use os.execv if you need to take over the running process else use subprocess.
I don't know about ureka, but for running any expression or command using python, you can certainly use the os module.
import os
os.system('eval `/Users/rem/.ureka/ur_setup -csh \!*`')
This Should work.

Lock windows workstation using Python

Is there a way to lock the PC from a Python script on Windows?
I do not want to implement some kind of locking on my own - I'd like to use the same lock screen that's also used when the user presses WIN+L or locks the machine via the start menu.
This can be done with the LockWorkStation() function from user32.dll:
This function has the same result as pressing Ctrl+Alt+Del and clicking Lock Workstation.
In Python it can be called using using the ctypes/windll FFI from the Python stdlib:
import ctypes
ctypes.windll.user32.LockWorkStation()
A good solution that makes us avoid using Libraries/DLL files is to use the command prompet/ power shell.
try running this command in your cmd rundll32.exe user32.dll, LockWorkStation....The PC is Locked!!
so we can use subprocess to run this command like this:
import subprocess
cmd='rundll32.exe user32.dll, LockWorkStation'
subprocess.call(cmd)
One more solution :
Run command to install the necessary package
pip install pyautogui
Run the below code
import pyautogui
from time import sleep
pyautogui.hotkey('win', 'r')
pyautogui.typewrite("cmd\n")
sleep(0.500)
pyautogui.typewrite("rundll32.exe user32.dll, LockWorkStation\n")

Run bash scripts from python. MAC OS

I am trying to run scripts for python in MAC OS.
I was unable to run scripts that run Bash.sh scripts, any one here have an idea about how can I run bash.sh From python in Mac OS.
Thanks you for any help :)!
(This is the scripts:
Called: RunScript.sh, and I need to run it from Python Script.)
Does os.system not work?
import os
os.system("/path/to/script.sh")
Presumably you would use the subprocess module.
The questions become:
Do you intend to capture output from this script?
Do you intend to feed input into it?
Does it need to interact with the user via the terminal (that Python is using)?
Do need to capture and or deal with error messages or error codes (return values)?

Categories

Resources