I have a shell file which I execute then, at the end, I get the possibility to press ENTER and run it again. The problem is that each time I press ENTER a new process is created and after 20 or 30 rounds I get 30 PIDs that will finally mess up my Linux. So, my question is: how can I make the script run always in the same process, instead of creating a new one each time I press ENTER?
Code:
#!/bin/bash
echo "Doing my stuff here!"
# Show message
read -sp "Press ENTER to re-start"
# Clear screen
reset
# Re-execute the script
./run_this.sh
exec $SHELL
You would need to exec the script itself, like so
#!/bin/bash
echo "Doing my stuff here!"
# Show message
read -sp "Press ENTER to re-start"
# Clear screen
reset
# Re-execute the script
exec bash ./run_this.sh
exec does not work with shell scripts, so you need to use execute bash instead with your script as an argument.
That said, an in-script loop is a better way to go.
while :; do
echo "Doing my stuff here!"
# Show message
read -sp "Press ENTER to re-start"
# Clear screen
reset
done
Related
I am trying to create a bash script to operate another bash script through CRON without the need for human intervention.
The script needs to be able to interact with the other script so that it accomplishes:
Enter
Press a number..
Then takes you to another section of the script where you need to enter another number..
Then enter another number..
Press enter again..
I can't get the script to hit Enter correctly. What I have so far, "echo | ./module1.sh" flickers, even tried "echo "\n"" which doesn't work.
#!/bin/bash
cd /home/usernamehere/scripts
echo | ./module1.sh
echo "1"
This script requires a person to sit at the terminal while it finishes what it needs to or be run in a tmux session with the user safely exiting the session.
If everything is read from stdin (as opposed to from the terminal device--which is what passwd and screen editors do), and the script requires you to enter ENTER, 1, 2 and 3, you can run it with
printf '\n1\n2\n\3\n' | ./module1.sh
An alternative is with a here-document (read your shell's manual page):
./module1.sh << EOF
1
2
3
EOF
I made simple script. file name is sutest.
#!/bin/bash
cd ~/Downloads/redis-4.0.1/src
./redis-server
echo "uid is ${UID}"
echo "user is ${USER}"
echo "username is ${USERNAME}"
I runed script.$ . sutest
But, script code is stopped at ./redis-server.
So I can't see echo messages.
I want to make this kind of script files. How can I do that??
I would be appreciate your help.
Let's say more general case.
myscript1 file executes process like redis-server above.
another myscript2 file executes process like redis-server above.
another myscript3 file executes process like redis-server above.
How can I run three script files simultaneously??
I want to do job in ssh connection.
To make the matter worse, If I can't use screen or tmux??
Add a '&' char at the end of the row
./redis-server &
this char permits to run in backgroud the job, and the script continues.
Just do the echos first:
cd ~/Downloads/redis-4.0.1/src
echo "uid is ${UID}"
echo "user is ${USER}"
echo "username is ${USERNAME}"
exec ./redis-server
The use of exec is a small trick (which you can omit if you prefer): it replaces the shell script with redis-server, so the shell script is no longer running at all. Without exec, you end up with the shell script waiting around for redis-server to finish, which is unnecessary if the script will do nothing further.
If you don't like that for some reason, you can keep the original order:
cd ~/Downloads/redis-4.0.1/src
./redis-server & # run in background
echo "uid is ${UID}"
echo "user is ${USER}"
echo "username is ${USERNAME}"
wait # optional
I have this command command that at some point, returns: Press Enter to continue.
I would like to write a script that calls command, reads the so-far command output, does something with it when Press Enter to continue arrives, and simulates an enter key-press when this is done.
Any chance I can achieve that? :-D
Something like:
myscript | command > output
with myscript
#!/bin/bash
cp output output2 # output2 should only contain the output until Press Enter to continue
echo -ne '\n'
except it doesn't work! :-)
Ideally you should use expect command to achieve that, example code:
#!/usr/bin/expect
set timeout 600
spawn command
expect "Press Enter to continue" { send "\r" }
Note: Replace command with your command.
then save it into the script, make it executable and run it.
Check: man expect for further information.
On OS X install via: brew install expect.
In my bash script, I do:
ssh me9#some_mad_server.com;
cd ~/apple;
echo "Before Exit"
exit
echo "After Exit"
I never see Before Exit or After Exit. I can understand why I may not see Before Exist as my script at that stage is in another console. But I am confused if the Exit mean my script ends and hence why After Exit never gets logged.
Any help appreciated.
To execute a series of commands on a remote host, you need to pass them to ssh on the command line, not execute them after the ssh call. Like this:
ssh me9#some_mad_server.com '
cd ~/apple
echo "Before Exit"
'
echo "After Exit"
This uses a multiline string to pass multiple commands. An exit is implicit when the end of the string is reached.
Importantly, the commands in the quoted string are executed on the remote host, while the final echo is executed on the local server. I've indented the remote commands for clarity.
You can use screen for this.
screen -d -m <command>
Use screen -r to attach to that screen again
screen -r <screen ID>
You have to excuse me if I use the wrong language here of if I'm asking an obvious but that is, after all, why I'm here.
I'm just getting to grips with shell scripting and have written a small script that is "Run as a custom command instead of my shell" to make things a little easier for the things I might want to do. Here's what I've got.
#
# Custom console startup script.
#
path_to_scripts=~/Scripts
echo "Hello $USERNAME, what would you like to do?"
echo "Options:"
echo "-l Continue in local machine"
echo "-s Connect to server"
read response
case $response in
"l") echo "Contunie within local machine.";;
"s") $path_to_scripts/connect_to_server;;
*) echo "Invalid command. Exiting.";;
esac
So my terminal starts up with this script and if I select 's' it runs the 'connect_to_server' script fine and connects then I'm in!
However when I enter an invalid command, or key in 'l' to exit and continue as normal the console says 'The child process exited normally with status 0.'
I know that it has just quit and the script has exited but what I want to do is just run the default shell so that I am then in my local machine at ~, as if id just started up console with default settings. What do I need to run in order to do this?
Run exec "$SHELL" to replace the current process with your normal shell.