How to open a new terminal by commands in a shell scripts? - shell

I have two set of commands to be executed from different directories from a shell script.
My content of the shell script is below:
echo "starting Windshaft cartodb..."
cd /home/user/Windshaft-cartodb/
node app.js development
echo "Windshaft cartodb started."
echo "starting CartoDB SQL API..."
cd /home/user/CartoDB-SQL-API/
node app.js development
echo "CartoDB SQL API started."
When I run the shell script file, the first 3 commands were running successfully. In order to run the next commands, I have to stop the previously running command by pressing Ctrl + C. Script processing continues with echo "Windshaft cartodb started." only after doing this.
My problem is: Without stopping the previously running commands, I need to execute the commands after the below commands in a new terminal.
echo "starting Windshaft cartodb..."
cd /home/user/Windshaft-cartodb/
node app.js development
How to open a new terminal by commands in a shell script?

How to open a new terminal by commands in a shell script?
You can open a new terminal with the command xterm and run a program in it with its option -e:
xterm -e node app.js development&

Related

Shell script closes iterm2 on exit

I need some help:
(On macos, bash shell)
If I run a .sh file which calls e.g. exit 1 (any exit code) my terminal session ends (and the iterm2 tab/window closes).
I'm calling the script like this $ . myscript.sh
I'm pretty sure it should not be like that or was not like this a while before.
Using:
. myscript.sh
You are actually running the script in the existing shell or "sourcing" the script. With exit at the end of the script, this means that the terminal session will also exit
Alternatively:
./myscript.sh
or
bash myscript.sh
Will run the script in a separate bash shell and stop the terminal session from exiting.
Instead of . myscript.sh you can run ./myscript.sh which will run it in a separate bash shell and will not exit the current session.
If you control the content of this .sh file, and you do want to source the script - simply return 1 instead of exit 1, and use proper error handling.

Run bash script loop in background which will write result of jar command to file

I'm novice to running bash script. (you can suggest me, if title I've given is incorrect.)
I want to run a jar file using bash script in loop. Then it should write the output of jar command into some file.
Bash file datagenerate.sh
#!/bin/bash
echo Total iterations are 500
for i in {1..500}
do
the_output="$(java -jar data-generator.jar 10 1 mockData.csv data_200GB.csv)"
echo $the_output
echo Iteration $i processed
done
no_of_lines="$(wc -l data_200GB.csv)"
echo "${no_of_lines}"
I'm running above script using command nohup sh datagenerate.sh > datagenerate.log &. As I want to run this script in background, so that even I log out from ssh it should keep running & output should go into datagenerate.log.
But when I ran above command and hit enter or close the terminal it ends the process. Only Total iterations are 500 is getting logged into output file.
Let me know what I'm missing. I followed following two links to create above shell script: link-1 & link2.
nohup sh datagenerate.sh > datagenerate.log &
nohup should work this way without using screen program, but depending on your distro your sh shell might be linked to dash.
Just make your script executable:
chmod +x datagenerate.sh
and run your command like this:
nohup ./datagenerate.sh > datagenerate.log &
You should check this out:
https://linux.die.net/man/1/screen
With this programm you can close your shell while a command or script is still running. They will not be aborted and you can pick the session up again later.

Mainscript executes subscripts in a new terminal window

"Debian 9 64x - LXDE"
I try to create an install script in bash. Lets assume i want to install samba. I call from the mainscript the install script for samba \folder\samba.sh. The script samba.sh should get executed in a new terminal window, so i can watch for install errors.
The script should work like follow description:
The script /mainscript.sh provides only user information, interaction, and executes multiple subscripts (/folder/subscripts.sh).
The script /mainscript.sh needs to create a new terminal window, passes the path, and the name of subscript.sh and executes them in the new terminal window.
The script /mainscript.sh must only execute one subscript (/folder/subscript.sh) at the time! If a subscript.sh is running then the mainscript must wait until the new terminal window gets closed.
The subscript.sh executes some code with root privileges.
Questions:
How can I create a new terminal window, pass the subscript, and execute it in the new terminal window?
How can I make sure that the script (mainscript.sh) only runs one subscript (subscript.sh) at the time?
Example:
mainscript.sh
#!/bin/sh
# This is the content of the mainscript.sh
# subscript1 and subscript2 must not be executed at the same time!
# the mainscript needs to wait when a subscript gets executed!
echo "hello, this script runs in terminal window (((A)))"
xterm /opt/subscript1.sh
echo "samba - Installed"
xterm /opt/subscript2.sh
echo "samba - removed"
subscript1.sh
#!bin/sh
# This is the content of the subscript1
echo "This script runs in a new terminal window (((B)))"
apt-get install samba
# instructions done .... close the terminal window (((B))) now
subscript2.sh
#!bin/sh
# This is the content of the subscript2
echo "This script runs in a new terminal window (((C)))"
apt-get remove samba
# instructions done .... close the terminal window (((C))) now
After clarification that you actually want a new terminal window to appear in LXDE here is a possible solution.
Debian LXDE is likely to have xterm or lxterminal installed. The example below is using lxterminal. For xterm use "xterm -e command"
Start by executing manscript.sh in its own window:
$ lxterminal --command=/mainscript.sh
#!/usr/bin/sh
<section that provides user information>
# Call subscripts that will run in sequence
lxterminal --command=/folder/subscripts.sh
When subscripts.sh finishes, the new terminal window will close and return control to mainscript.sh
You get only one subscript to run at a time by calling these in sequence.

Shell commands working from PuTTy but not from Shell script File

I am trying to execute the below shell commands from PuTTY
PACKAGEPATH=/data/dev/bwan/deliveryPackage
RELEASENAME=R201709TEST3
PACKAGEFINALPATH=$PACKAGEPATH/$RELEASENAME
echo $PACKAGEPATH
echo $RELEASENAME
echo $PACKAGEFINALPATH
and I get the desired output as
/data/dev/bwan/deliveryPackage
R201709TEST3
/data/dev/bwan/deliveryPackage/R201709TEST3
However, If I am putting the same steps in a shell File and then executing that shell Script, it is giving me unexpected responses as
/data/dev/bwan/deliveryPackage
R201709TEST3
/R201709TEST3n/deliveryPackage
Why is this happening?

Run a bash script via another bash script to delete a file is not working properly

I have a bash script start.sh which calls another run.sh, which takes me to another prompt where I have to delete a file file.txt and then exit out of that prompt.
When I call run.sh from inside start.sh, I see the prompt and I believe that it deletes the file.txt but the inner/new prompt waits for me to exit out of it while the script is running - meaning it needs intervention to proceed. How do I avoid it in bash?
In Python I can use Popen and get it going but not sure about bash.
EDIT: I would rather like to know what command to provide to exit out of the shell (generated from running run.sh") so I can go back to the prompt where "start.sh" was started.
Etan: To answer your question
VirtualBox:~/Desktop/ > ./start
company#4d6z74d:~$ ->this is the new shell
company#4d6z74d:~$ logout ---> I did a "Control D here" so the script could continue.
Relevant part of start.sh which:
/../../../../run.sh (this is the one that takes us to the new $ prompt)
echo "Delete file.txt "
rm -f abc/def/file.txt
You can run run.sh in the background using &. In start.sh, you would invoke the script via /path/run.sh &. Now, start.sh will exit without waiting for run.sh to finish (which is running in the background).

Resources