How to run foreground script after autologin to raspbian CLI - raspberry-pi

I have python script that runs in infinitve loop and gets input from keyboard. Works like a charm when I run it manually from CLI. I want to automate this process -> that it will start up automaticly after Rpi boot and login "pi" user.
I used raspi-config to setup autologin to CLI as "pi" user and then I want to run python script in foreground.
Right now Rpi boots -> login as pi user to CLI -> and waits for action. And here I want to have my script start
I tried to run it as a service or in crontab but it always runs in the background. I also tried running it through /etc/rc.local but it starts running before the login prompts which i don't want. I found some tutorials but all of them are for GUI and background processes. I thought that it will be quite easy but after several hours I decided to ask here.
My OS is
Distributor ID: Raspbian
Description: Raspbian GNU/Linux 9.8 (stretch)
Release: 9.8
Codename: stretch
I run script -> python keyboardinput.py

I would invoke your script from /home/pi/.bashrc
Your script would run every time you login and a shell starts.

Related

How can I activate a service after the desktop on raspberry pi (not immediately with boot)?

I want to launch an executable script python automatically. How can I activate a service after the desktop on raspberry pi?
I want to launch an executable script python automatically. How can I activate a service after the desktop on raspberry pi?
When I launched the service at the boot, it's failes (need some library python).
I'll try autostart, sudo nano /etc/rc.local and service.
Nothing works.
So, the answer i think is to launch the script automatically after boot.
Thanks a lot for your help
You can insert the script run in crontab, for it to run after a few seconds upon reboot. For example:
#reboot sleep 20 && /usr/bin/python my_script.py
Hope it helps!

How to enable test mode when deploying Windows 10 wim image with Dism?

I am upgrading about 200 machines in my lab from Windows 7 to Windows 10, and as part of the upgrade, I am also converting the file system on the machines to GPT.
I am doing this as an automated process with WinPE images that are loaded from my PXE server. In the image, there is a script that formats the hard drive with Diskpart, creates EFI boot partition and the OS partition, deploys the image like this:
*dism /Apply-Image /ImageFile:M:\Images[image file name].wim /Index:1 /ApplyDir:W:*
And after deployment, it runs bcdboot W:\Windows command so the PC will boot into Win10, then reboots the PC from hard drive with freshly deployed OS image.
Now it works fine, except for one problem:
For our needs (this is a testing lab), we use a proprietary driver, that is unsigned, and thus needs to run Windows in Test Mode.
With Win7 and MBR, I didn't have this problem, because I used Ghost to take the whole HDD image and just dump it on the HDD, without needing to overwrite the boot script.
Now, the bcdboot W:\Windows disables the test mode, and I am getting a BSOD when loading the said driver, because of it.
How can I enable Test Mode when deploying with DISM, before booting into OS, using command line? Is there a way to do it with bcdboot command somehow?
I have to automate it, because I need to do it on 200 machines.
The OS is Windows 10 RS4 x64 Enterprise.
Thanks in advance for the answer.
Found sort of solution.
If test mode is not enabled, then Windows 10 just starts with the unsigned drivers disabled, unlike 7, which gave BSOD on startup.
So the test mode can just be re-enabled after that with:
bcdedit /set testsigning on and shutdown -r -t 0 to restart the machine
Still would like to know if there is an option to re-enable test mode before booting into Windows.

How to run Google Assistant library (on AIY kit), upon startup of Raspberry Pi?

We set up a voice kit using Raspberry Pi (using "the MagPi essentials AIY Projects" manual). We are able to enable Google Assistant using the command "src/assistant_library_demo.py" in the dev terminal, after Raspberry Pi starts up. We would like to embed the voice kit in a stuffed animal with a portable power supply (i.e., used to charge cell phone on the go). But when the portable power supply is charged, the Raspberry Pi resets. That requires us to go back into the Raspberry Pi, open the dev terminal, and run the Google Assistant file.
My question: Is it possible to run a startup script that automatically runs Google Assistant upon Raspberry Pi starting up? How to do this?
I ended up creating a crontab job after a 10 second wait. Starting right at boot didn't give it enough time for the internet to connect fully.
In terminal type:
crontab -e
Choose an option if it asks how you want to open/edit the file. Then at the bottom put:
#reboot sleep 10 && /home/pi/pathtofile > /home/pi/cronlog 2>&1
Save the file and reboot or pull the cable out and plug it back in. The cronlog helped me troubleshoot this whole process and get feedback on why it didn't work.
Take a look at this page. It tells you how to set up a service which will run automatically.
If the link has gone bad, here is a short explanation of it:
Create a file called my_assistant.service in the src directory, and put in the following code
[Unit]
Description=My awesome assistant app
[Service]
Environment=XDG_RUNTIME_DIR=/run/user/1000
ExecStart=/bin/bash -c 'python3 -u src/my_assistant.py'
WorkingDirectory=/home/pi/AIY-projects-python
Restart=always
User=pi
[Install]
WantedBy=multi-user.target
Where the file says src/my_assistant.py, replace my_assistant with your program's filename. Now go to the folder that file the .sevice file is in, and run the command sudo mv my_assistant.service /lib/systemd/system/. This code moves the file to the services folder. Now you can run the following commands to change the service:
Enable the service- sudo systemctl enable my_assistant.service
Disable it- sudo systemctl disable my_assistant.service
Start it (just runs it once, enabling makes it run on startup)- sudo service my_assistant start
Stop it- sudo service my_assistant stop
See the logs, when the program was started and if an error occurred- sudo service my_assistant status

Run the Mechanize::Firefox script in VNC server using Net::SSH::perl?

I have done the script with Mechanize::Firefox. I need to run the script in VNC server. I'm working on ubuntu. I have installed krdc software to run the scripts.
Before running the script I'm starting the vnc server by using the following command vncserver.
To connect the server via ssh I have using one password for example sshpassword. When open the krdc software there I'm using the vncpassword.
My problem is, I have trying to run the Mechanize::Firefox script using Net::SSH::perl script it is running but the firefox is not starting. So I cannot crawl the content.
How can I fix this issue.? I don't know from where need to start.

How to make a server daemon which re-runs automatically when they're terminated unexpectedly?

I'm trying to running OrientDB on Ubuntu. Currently, I'm running with bin/server.sh. This works fine except it runs foreground on shell. I can make it work background by Ctrl+Z and bg command, but this doesn't mean it's running as daemon.
I wish the program will keep running after I logout. And will be started again when it terminated unexpectedly or OS restarts. Like MS Windows Services. But the problem is I don't know how can I do this.
How can I run a program as a long-running service?
If you do not own the server, look into using the "screen" command. It will allow you to run a command, detach from the console where the command is running, then log out while leaving it running. You may reconnect to the running screen to see output or restart the script. Here's more info about the screen command:
http://www.manpagez.com/man/1/screen/
If you own the server, you should write an init script. It's not very hard, and you can set it up to run automatically on startup. The system will run the script with a "start" parameter when you want it to start, and a "stop" parameter when you want it to stop. Here's more detailed information:
http://www.novell.com/coolsolutions/feature/15380.html
If the command doesn't already detach from the console (run in daemon mode) then in the init script place the command in parenthesis to run in it's own shell. You will not see any output unless you pipe it to a file within the parenthesis.
(bin/server.sh >> /var/log/server.log)