No qemu-kvm console output when I run kvm inside bash script - bash

I'm running kvm image from command line;
$ qemu-kvm -name openSUSE -M pc -m 2048 -drive file=hda -nographic
And I configured console=ttyS0 inside kvm image, so the kvm's console output is displayed in the host terminal output.
But when I try the same command in bash script, nothing displayed!
Can you please let me know how to capture kvm's console output from basn script?

be sure you script start with
#!/bin/bash
And set the full path to you disk file, for example
#!/bin/bash
qemu-kvm -name openSUSE -M pc -m 2048 -drive file=/foo/hda -nographic
Then set execution permission with
chmod +x script.sh
now try with
./script.sh
Cheers

Related

How do I open an new WSL console in my current wsl console?

Im currently writing a script which should start a docker container in a new WSL console. My question is: How do I open a new WSL console in my current working directory and execute a command in it without blocking my current console?"
I already tried "wsl -d" and "bash -c". The command "wsl" was not found and "bash -c" only esecute the given command in the current console.
Here is my script for bettter understanding:
wsl -d Ubuntu -e "sudo dockerd"
sleep 5
echo "y" | sudo docker system prune -af
wsl -d Ubuntu -e "sudo docker-compose -f docker-compose.development.yml up"
sleep 5
sudo ./connect_gremlin.sh

How to run two docker containers from cron?

I am trying to set up crontab to run two docker containers on system startup/reboot. The line I use to do this after entering the command crontab -e is:
#reboot sh folder_b/run_docker_containers.bash
The script run_docker_containers.bash has the following contents:
#!/bin/bash
# Run containers based on setup_image and main_image
sudo bash /home/user/folder_a/run_setup_docker_container.bash
sudo bash /home/user/folder_b/run_main_docker_container.bash
The scripts run_setup_docker_container.bash and run_main_docker_container.bash both have the following contents (where docker_image is setup_image and main_image, respectively):
#!/bin/bash
/snap/bin/docker run \
--rm \
--detach \
--privileged \
--net=host \
--device /dev/bus/usb \
docker_image:latest \
/bin/bash -c\
"
*SOME COMMANDS*
"
export containerId=$(/snap/bin/docker ps -l -q)
However, the containers are not run when the script is executed on reboot. I prove it finds the script folder_b/run_docker_containers.bash by adding the following code to it and seeing that the new file has been created after reboot.
touch proof_that_crontab_has_done_something.txt
It seems that crontab cannot find the scripts run_setup_docker_container.bash and run_main_docker_container.bash. Any ideas where I'm going wrong?
If you want to execute a shellscript with sudo rights I would recommend using the sudo crontab.
sudo crontab -e
Your personal cronjob should not be able to start a shell with sudo rights. Unless you do some weird modifications.
Use the absolute path
#reboot /...../folder_b/run_docker_containers.bash

Shell Script - SSH using Screen

I am trying to develop a shell script / bash script with ssh and screen, using Ubuntu / Linux.
Could you help me to correct what is wrong here?
#!/bin/bash
ssh -i keypair.pem -t ubuntu#ec2-address "screen -S teste"; cd 'home'; cd 'Shell'; ./'AWS - teste.sh' "
It doesn't work.
You fell victim to shell quoting - which is tricky when using ssh. Your command does something alike
cd home/Shell; ./AWS - teste.sh
so I would try
ssh -i keypair.pem -t ubuntu#ec2-address screen -S teste "sh -c \"cd home/Shell; ./AWS - teste.sh\""

How can I fix "bash: screen: command not found" in shell file

Whenever I run this code I always get the error "bash: screen: command not found"
The code's supposed to run spigot.jar, while also doing a few other things (not my code)
I really don't know what to try, but I do think I might have to be using linux (which would be a pain in the ass figuring I only have one computer)
screen -S powercraft/PRISON -p 0 -X stuff "`printf "stop\r"`" ;
screen -S root/PRISON -p 0 -X stuff "`printf "stop\r"`" ;
sleep 10 ;
pkill -f PRISON ;
cp -r /home/ALL/update/plugins-1.8/* plugins ;
cp -r auto/* plugins ;
sleep 1 ;
rm -rf auto/* ;
screen -d -m -S PRISON java -server -Xmx6G -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:MaxGCPauseMillis=30 -XX:+UseBiasedLocking -XX:+OptimizeStringConcat -XX:+UseFastAccessorMethods -XX:+AggressiveOpts -jar spigot.jar
Looking at your code, I can see the command pkill. pkill is a Linux command not available on Windows. You don't have to install linux on your pc. You can use a virtual machine or you can install linux subsystem on windows 10. Instructions on the later can be found here

Need to run chromium as normal user from root script

I have a kiosk that shuts down every day using rtcwake, and this uses root user. I've used && to execute the boot script after rtcwake completes, however it then starts the browser as root causing problems.
This is the command I use:
echo debian | sudo -S rtcwake -m mem -u -t $(date +%s -d '3 days 7:45') && sudo -u debian -i bash $HOME/kiosk/bin/startup.sh &.
The sudo command does work to some extent. It calls the debian user, and executes the correct script, however, it still screws up my chromium preferences.
Here is the startup script:
echo debian | sudo -S hwclock -w
export HOME=/home/debian
#log boot time
echo "Booting at" $(date) >> $HOME/kiosk/bin/logs/boot.log
#echo debian | sudo -S service connman restart
echo debian | sudo -S at 15:30 -f $HOME/kiosk/bin/shutdown.sh
crontab -u debian crontab.txt
bash $HOME/git.sh
#sudo -i -u debian
#start kiosk
export DISPLAY=:0
chromium-browser --kiosk --disable-gpu
http://localhost/kiosk/Client/main.html &
#update ip
bash /home/debian/git.sh &
I'm wondering what could be causing chrome to be executed as root. I have no idea what is going wrong.
If you execute a command with sudo it will not change environment variables like $HOME. Since per user settings are stored in $HOME, this affects the executed program if it needs such configuration files. Check this for example:
sudo -u debian bash -c 'echo $HOME'
It will print the home folder of the calling user, not the home folder of the user specified trough -u. The sudo command supports the -H command line option to handle this, however if it works depends on the security police in use.
As a solution you can use the su command instead of sudo in this case:
... && su debian -c chromium
Since su itself is executed by root you won't be asked for the password.
You must enter a password to log into a new user shell.
The command needs to be modified as follows:
echo debian | sudo -S rtcwake -m mem -u -t $(date +%s -d '3 days 7:45') && echo debian | sudo -S -u debian -i bash $HOME/kiosk/bin/startup.sh &
This avoids needing a password to log in as normal Debian user, and executes the script.

Resources