Bash Simulate User input for terminal gui program - linux

I have been trying to automate the setup process for pi hole on a raspberry pi. I am relatively new to bash and am unable to figure out how to automate the setup process once the terminal gui for the program begins.
this is what I have so far
#!/bin/bash
pihole -r
echo "waited"
$SHELL
So I reiterate back to my question, how do I automate the task of choosing and entering the option in the terminal gui of the pihole program? Any help would be greatly appreciated.

There's already a script that should be able to do it for you ("Pi-hole Automated Install")
To execute it you'd do:
curl -L install.pi-hole.net | bash
unfortunately it's too big to post here, so you'll want to grab it
here:
↳ Github : Pi-hole Automated Install

Related

Playing a video on Raspberry Pi OS startup

I need to run a script to play a video when I turn on my Raspberry Pi 4. I'm using crontab to run my script which opens a video with mpv.
When I run the script normally, it works fine and the video is being played.
The problem is, when i boot the raspberry, the script automatically runs but mpv doesn't .
here is my script:
#!/bin/bash
VIDEOPATH="/home/pi/Desktop/my-movie.mkv"
SERVICE="mpv"
while true; do
echo "playing "+$VIDEOPATH
$SERVICE --fs --start=00:00:00 $VIDEOPATH
done
I added this line to crontab:
#reboot /home/pi/Desktop/my-script.sh
I'm totally stuck in this. Any help saves my life!
The most practical solution I found to run GUI programs on startup is using Autostart.
I created a .desktop file at /etc/xdg/autostart directory:
sudo nano /etc/xdg/autostart/display.desktop
when display would be a custom name for my script.
I added following lines the display.desktop :
[Desktop Entry]
Name=Play a video
Exec=mpv --fs --start=00:00:00 path-to-my-video
Saved the file and reboot the Pi.
sudo reboot
As soon as my Pi boots up, my GUI program automatically start as well.
Please update your script:
#!/bin/bash
source ~/.bash_profile
VIDEOPATH="/home/pi/Desktop/my-movie.mkv"
SERVICE="mpv"
while true; do
echo "playing "+$VIDEOPATH
$SERVICE --fs --start=00:00:00 $VIDEOPATH
done

Linux - shutdown-script with SSH

I would like to make a shutdown-script for my raspberry pi to shut down anothe raspberry pi over ssh.
The script works if it is running itself but at the shutdown routine the ssh command is not executed.
So that I have done until now:
Made the script in /etc/init.d:
#!/bin/sh
# the first thing is to test if the shutdown script is working
echo "bla bla bla " | sudo tee -a /test.txt
ssh pi#10.0.0.98 sudo shutdown -h now
Made it executable
sudo chmod +x /etc/init.d/raspi.sh
Made a symlink to the rc0.d
sudo ln -s /etc/init.d/raspi.sh /etc/rc0.d/S01raspi.sh
Now I know so far that the shutdown script is working outside of the shutdown routing by calling itself and the shutdown symlink I made is also working partially because I see the changes in the test.txt file every time I shut down.
Can anyone help me how to solve my problem?
Have you tried with single quotes?
The first link in Google has it
http://malcontentcomics.com/systemsboy/2006/07/send-remote-commands-via-ssh.html
What about the sudo, how do you solve entering the password?
https://superuser.com/questions/117870/ssh-execute-sudo-command
Please check this or other links on the web that have useful information.
I would have send all this in a comment but I cant yet because of reputation.
I have now got the script running by myself. I do not really know why it is now working but I write it down beneath and maybe someone else can clearifiy it.
I don´t think the first two changes at my system makes a difference but I also write it down. In the meanwhile because I do not managed the script to get working I had made a button to shutdown the system manually. Also I made a script which backs the mysql-database up (which is on the Raspberry Pi which I would like to switch off with the script) and copies the backup to the raspberry pi which should switch of the other raspberry automatically via the shutdown-script. This happens with scp and also for the password is a key generated.
I have also changed my script to get a log-message out of the script.
#!/bin/sh
ssh -t -t pi#10.0.0.99 'sudo shutdown -h now' >> /home/osmc/shutdown.log 2>&1
To get it into the shutdown-routine I used:
sudo update-rc.d raspi-b stop 01 0
I hope somebody can say me why my code now worked on the first day but not on the next few days until now.
I structured a command to suspend or shutdown a remote host over ssh. You may find this useful. This may be used to suspend / shutdown a remote computer without an interactive session and yet not keep a terminal busy. You will need to give permissions to the remote user to shutdown / suspend using sudo without a password. Additionally, the local and remote machines should be set up to SSH without an interactive login. The script is more useful for suspending the machine as a suspended machine will not disconnect the terminal.
local_user#hostname:~$ ssh remote_user#remote_host "screen -d -m sudo pm-suspend"
source: कार्यशाला (Kāryaśālā)

create screen session that doesn't terminate with the program

I'm working on a startup script that is initiated from rc.local. I start up several programs with
screen -d -m my-prog
and that works great. However, if one of the programs has problems and exits, so does the session. I'd like to be able to have the session stick around so I can attach to it and see the output from the program before it crashed.
Is there a way to do this? I thought about
screen -d -m bash -c my-prog
But again, if my-prog terminates then so does bash and then so does screen.
You can follow the answer at https://unix.stackexchange.com/questions/47271/prevent-gnu-screen-from-terminating-session-once-executed-script-ends
They suggest something like you were trying in your second attempt, but instead of using bash to invoke the command (which terminates with the command as you noted), invoke bash after the command finishes like:
screen -dmS session_name sh -c 'my-prog; exec bash'

Having problems running python script even when putty is closed

So basically, I have a bot I'm running, and I would like for it to keep running even when I exit putty.
I've tried using nohup python bot.py & but it still ends the python bot when I close the putty program. I've also tried using a run.sh file with /usr/bin/nohup bot.py & inside it. but it won't work :( is there something else I'm missing?
I have also made sure the run.sh is a executable as some other forums have suggested, and I still get can't open run
I'm kinda new to the linux terminal.
if you guys could help me out that would be awsome :)
You need to detach the terminal so that when you exit, it is still running. You can use screen or tmux or some other multiplexer.
Here is how to do with screen:
screen -S mybot -m -d /usr/bin/python /path/to/bot.py
-S give the session a name (this is useful if you want to attach later. screen -D -R mybot)
-m always create a new session
-d detach (launch the program, but then detach the terminal returning you to the prompt)

Run a c-programm on startup and see echo via ssh on Raspberry Pi

I have found many ways of starting a script or program when booting Linux, but none of the options is sufficient for what I am looking for:
I have a Raspberry Pi running raspbian wheezy
I have a compiled c-program which creates echo-outputs from time to time
I would like the program to run on startup of the pi and be able to connect via ssh and see what is going on in the program (get echo-live-output, not only a log-file).
Is there any way of achieving this?
To run the program at startup, put it in the root crontab and use the #reboot option. Why don't you want to redirect the output to a log file? Then you can monitor it's output in many ways (eg. using scp).
For anyone encountering a similar problem: I solved it the following way:
I created a startup-bash-script including
sudo /foo/main.o | tee /foo/log;
which writes the output into a log-file. Since the output is created in an infinity-loop, one has to make sure to include a fflush(stdout); after every printf()
Then I can monitor the log-file by using
tail -f /foo/log

Resources