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

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

Related

Issuing Command Via SSH Prompts for Password

I'm having an issue with a script used in a project I inherited that has little to no documentation, and am in the process of documenting everything. I'm trying to debug an issue with one line of a script that is executed on the host machine to call out to a LAN-attached Raspberry Pi with SSH to return some information about the Pi.
We already have working versions of this Raspberry Pi which can execute the script without issue, and I'm not sure what the difference is. When executed on the new one, it prompts for the root password on the Pi, but it has not done this on previous versions of the device. I assume it has something to do with the SSH configuration but I don't know enough about SSH to say what would be the cause.
The line in particular causing the issue is:
ssh -o StrictHostKeyChecking=no {host_name} uname -a &>/dev/null
rc=$? #gets the return value of the remote command so we can read the uname info
{host_name} of course is the actual host name it's connecting to, but I've left that part out for privacy reasons. The script is the same on both machines.
Both Pi devices are the same model and I'm having trouble narrowing down what could cause me to not be able to execute this command. Does anyone know what I need to configure in order to be able to execute this command on the Pi remotely?
Quick fix:
sshpass -p 'password' ssh -o StrictHostKeyChecking=no user#server
Detailed fix:
Most likely you would need to set up Async keys (public/Private) for proper passwordless login. Your command does not show you are using keys so I'm assuming you are not (e.g. -A or -i /path/to/key). Generally root user is blocked (I guess not your problem), I would set up another user for this or change sshd config. You could also Compare the sshd configurations between the Pi Boxes.
See: https://www.raspberrypi.org/documentation/remote-access/ssh/passwordless.md
Okay, so after some more digging around, I discovered that there was a separate .ssh directory under /root that contained an authorized_keys file. After copying this to the new Pi, it worked. I had been wondering all this time if there was a separate config folder for root, but I've never gone digging around /root, so I wasn't aware that it was there.

Bash Simulate User input for terminal gui program

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

How to Run Shell Script at start in VM Virtualbox Ubuntu 16.04

I am trying to run a shell script which runs at system login. To try, I used an example script, which has two lines
#!/bin/bash
echo “Hello World”
I Followed all the instructions mentioned on this website http://www.cyberciti.biz/tips/linux-how-to-run-a-command-when-boots-up.html
Even I did some steps such as editing /etc/rc.local, but still when I login, I do not see Hello world output running on terminal.
Can anyone please explain what is wrong here or may be I am missing something?
Looks to me like Ubuntu 16.04 is a systemd system.
Which means you should create a systemd service to run whatever you'd like # startup.
Look here https://wiki.ubuntu.com/SystemdForUpstartUsers#Example_Systemd_service
After you made your service, use systemctl to enable it on boot for systemd.
sudo systemctl enable mycustom.service
To start the service.
sudo systemctl start mycustom.service
You can also schedule a cron job to run once after the reboot command. The line will need "#reboot root /path/script.sh" You can specify whatever user you want to run the script. To make the result easier to find, you might make that second line something like
echo "Hello World" > /root/hello.txt
Add the line in /etc/profile under echo command as follows
echo “Hello World”
Then execute the following command
source /etc/profile

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ā)

RPI Not Booting because of infinite shell script

so today i was trying to get a shell script(which is an infinite loop) to run on boot in the background of my RaspberryPi's terminal.
I used this command: update-rc.d -f GPIOServer.sh start 4
and then rebooted my pi and after a couple seconds it runs the infinite loop and doesn't boot to the terminal.
i don't know how to cancel the script: ive tried ^C ^Z ^X Esc and i dont want to have to erase all my files etc.
Please help.
There are several options to get a shell without completing the full boot process:
Try switching to a different console for example with CTRL+ALT+F2 (or any other F2-12 key). This will only work if your init script is one of the last scripts to start.
If the ssh server started before your script then you will be able to connect remotely from another computer
Follow Ignacio's recommendation: by taking out the SD card and mounting the SD card on another linux computer and deleting your init script
If you only have a mac or windows machine then you will only be able to edit the boot parition of the SD card. In this case you can modify/add the boot config file cmdline.txt to boot straight into a root terminal. If there is already a cmdline.txt file on the the sd-card then make a backup copy of this file and then simply add init=/bin/bash after the root kernel parameter.
For example, the full line might look something like this:
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p3 init=/bin/bash rootfstype=ext4 elevator=deadline rootwait
If there is no cmdline.txt file on the sd-card then create a new empty text file and just put init=/bin/bash into that file. Your raspberry pi will now boot straight into a root terminal.
Assuming you are not logging in as root. You can try to log in as root and disable the infinite loop for your user's shell.

Resources