I am facing some trouble in running a shell script in my Ubuntu 16.04 from a Qt GUI application. onClick() for the push button is given below, which i found from How to start a Shell Script with QProcess?
void MainWindow::on_pushButton_clicked()
{
QProcess process;
process.startDetached("/bin/sh", QStringList()<< "/home/rahul/ifak/generateCAM.sh");
}
But unfortunately, this is not working in my case. When i replaced "gedit" with "/bin/sh", the script is opening up in gedit. Is this an issue with the way i am using "/bin/sh"?
OR am i missing something else?
Please give your valuable suggestions. Thanks in advance.
#!/bin/bash
SESSION=OpenC2X
CURR_DIR=$(pwd)
OPENC2X=$CURR_DIR/..
BUILD_DIR=$OPENC2X/build/
GLOBAL_CONFIG=$OPENC2X/common/config/config.xml
LOCAL_CONFIG_RELATIVE=config/config.xml
LOGGING_CONF=config/logging.conf
STATISTICS_CONF=config/statistics.conf
tmux -2 new-session -d -s $SESSION
tmux set-option -g mouse
tmux new-window -t $SESSION:1 -n 'App'
tmux split-window -h
tmux select-pane -t 0
tmux send-keys "cd $BUILD_DIR/cam/src" C-m
tmux send-keys "./cam $GLOBAL_CONFIG $OPENC2X/cam/$LOCAL_CONFIG_RELATIVE $OPENC2X/cam/$LOGGING_CONF $OPENC2X/cam/$STATISTICS_CONF" C-m
tmux split-window -v
tmux send-keys "cd $BUILD_DIR/httpServer/src" C-m
tmux send-keys "./httpServer $GLOBAL_CONFIG $OPENC2X/httpServer/$LOCAL_CONFIG_RELATIVE $OPENC2X/httpServer/$LOGGING_CONF $OPENC2X/httpServer/$STATISTICS_CONF" C-m
tmux split-window -v
tmux send-keys "cd $BUILD_DIR/ldm/src" C-m
tmux send-keys "rm ../db/ldm-*.db" C-m
tmux send-keys "./ldm $GLOBAL_CONFIG $OPENC2X/ldm/$LOGGING_CONF $OPENC2X/ldm/$STATISTICS_CONF" C-m
tmux split-window -v
tmux kill-pane
tmux select-pane -t 3
tmux send-keys "cd $BUILD_DIR/denm/src" C-m
tmux send-keys "./denm $GLOBAL_CONFIG $OPENC2X/denm/$LOGGING_CONF $OPENC2X/denm/$STATISTICS_CONF" C-m
tmux split-window -v
tmux send-keys "cd $BUILD_DIR/dcc/src" C-m
tmux send-keys "sudo ./dcc $GLOBAL_CONFIG $OPENC2X/dcc/$LOCAL_CONFIG_RELATIVE $OPENC2X/dcc/$LOGGING_CONF $OPENC2X/dcc/$STATISTICS_CONF" C-m
tmux split-window -v
tmux send-keys "cd $BUILD_DIR/obd2/src" C-m
tmux send-keys "./obd2 $GLOBAL_CONFIG $OPENC2X/obd2/$LOCAL_CONFIG_RELATIVE $OPENC2X/obd2/$LOGGING_CONF $OPENC2X/obd2/$STATISTICS_CONF" C-m
tmux split-window -v
tmux send-keys "cd $BUILD_DIR/gps/src" C-m
tmux send-keys "./gpsService $GLOBAL_CONFIG $OPENC2X/gps/$LOCAL_CONFIG_RELATIVE $OPENC2X/gps/$LOGGING_CONF $OPENC2X/gps/$STATISTICS_CONF" C-m
tmux -2 attach-session -t $SESSION
Related
here is my test.sh:
#!/bin/bash
tmux kill-session -t django
tmux new-session -s django -d
tmux send-keys -t django "cd ~/DialogueRobot/DiaRobot" C-m
tmux send-keys -t django "conda activate robot" C-m
tmux send-keys -t django "python manage.py runserver 0.0.0.0:8000" C-m
i have checked my server by running tmux ls and test.sh actually works.
but it just can't exit when i run ssh username#ip 'bash test.sh' in local shell unless i input Ctrl+C.
i can't figure out why this happens.
I've tried just about everything I can find online, but nothing is working. I have tried the following methods, and the usual result is a new tmux session with only one window.
Simply in .bashrc.
.bashrc
tmx () {
tmux new-session -A -s SessionName
tmux new-window -n Win1
tmux new-window -n Win2
tmux new-window -n Win3
tmux new-window -n Win4
tmux attach-session -d -t SessionName # with and without this line
tmux select-window -t Win1 # with and without this line
}
And again only in .bashrc.
.bashrc
tmx () {
tmux new-session -A -s SessionName ||
tmux \
neww -s Win1 \; \
neww -s Win2 \; \
neww -s Win3 \; \
neww -s Win4 \; \
selectw -t Win1
}
This following attempt would be my preferred method, as it makes the most sense to me.
Calling tmux without the first line makes all the other lines cause a "Session not found" error to occur. This makes no sense, since aren't we supposed to call tmux in order to reach this stuff anyway? My original plan was to make a session and have this file automatically set up my tmux.
.tmux.conf
new-session -A -s SessionName
new-window -t Win1
new-window -t Win2
new-window -t Win3
new-window -t Win4
attach-session -d -t SessionName # with and without this line
select-window -t Win1 # with and without this line
This method, whether using an alias or making a function, usually results in "failed to connect to server". But when fiddling with it enough for that not to happen, it produces the same result as the rest.
.bashrc
alias tmx='tmux source-file "~/.tmux/mysession"'
.tmux/mysession
new-session -A -s SessionName
new-window -t Win1
new-window -t Win2
new-window -t Win3
new-window -t Win4
attach-session -d -t SessionName # with and without this line
select-window -t Win1 # with and without this line
What am I doing wrong?
You need to create the session in detached mode (-d); otherwise, your script blocks until you detach from the new session. Likewise, your script will block after tmux attach-session until you detach, so you need to select the correct window first. Note that you can -d with new-window to avoid making each new window the current window, eliminating the need to call select-window at all.
Yes, -d gets used a lot.
tmx () {
# Use -d to allow the rest of the function to run
tmux new-session -d -s SessionName
tmux new-window -n Win1
# -d to prevent current window from changing
tmux new-window -d -n Win2
tmux new-window -d -n Win3
tmux new-window -d -n Win4
# -d to detach any other client (which there shouldn't be,
# since you just created the session).
tmux attach-session -d -t SessionName
}
I have the following script:
#!/bin/sh
# Startup tmux session, activate env inside session, wait, execute invoke command
DIR=$(echo $DIR_HOME)
CLOUD=$'cloud_sql'
tmux new -s $CLOUD -d
printf "Starting: $CLOUD \n"
tmux send-keys "cd $DIR_HOME" C-m
tmux send-keys 'pipenv shell' C-m
printf 'Env Started! \n'
if [[ "-z ${ENV_ACTIVE}" ]]; then
tmux send-keys -t $CLOUD "wait 15" C-m
tmux send-keys -t $CLOUD 'inv gce.cloud-sql-proxy -p 5432' C-m
else
tmux send-keys 'Server not ready!'
fi
printf "$CLOUD started. Attach using: \n\n"
printf "tmux attach -t $CLOUD \n\n"
exit 0
I'd like for the pipenv shell command to finish before (inside the tmux session) running inv gce.cloud-sql-proxy .. but for some reason I see it executes those commands before/during the activation of pipenv shell..
On a side note, if I added a tmux attach -t $CLOUD before the if statement, it seemed to work although this was ideal and id like to exit the tmux session upon completion.
Thanks for your help!
I had the same problem, for me it was:
tmux send-keys "pipenv shell" C-m
tmux send-keys "python run.py" C-m
I solved it by appending the following to my Pipfile:
[scripts]
serve = "python run.py"
Then I changed my tmux to:
tmux send-keys "pipenv run serve" C-m
This runs my script called serve, i.e., runs a program using the env without needing to load it first. You can probably do the same by creating a script in Pipfile for inv gce.cloud-sql.proxy... and run that.
I'd like to start tmux from terminal, split it window and execute in the splits some commands. Furthermore, I'd like to did with bash script, that I start from terminal. Now, I've been able to start tmux only.
#!/bin/sh
tmux
# How to split tmux window there, and start commands in splits?
There is a script started tmux, splitted for to panes and started ls in the left pane and top in the right one.
#!/bin/sh
tmux new-session -d -s main ;
tmux split-window -h ;
tmux select-pane -L
tmux send-keys -t main 'ls' C-m
tmux select-pane -R
tmux send-keys -t main 'top' C-m
tmux attach-session -d -t main
In tmux command mode, the following creates new window and opens vim inside:
:new-window vim
When you quit vim, the window is also closed. Is there a way to make it stay?
tmux has an option for this: remain-on-exit:
tmux set remain-on-exit on
I realise this is a long dead question. But I'm a recent user of tmux and I had this same question. It turns out that you might want to do this:
tmux new-session bash -l
That gets you a bash window (login shell). Then, run whatever commands you want. When they're done, they return to a command prompt, like you expect. I think a lot of people are looking for an interactive 'screen'-like behaviour. This is an easy way to do that.
I use send-keys so that it "types" the command into the shell. Here's my get.all script, which fires up many commands, some of which I may need to interact with after they're done (and the ones I don't, have exit):
#!/bin/sh
tmux att -t get ||
tmux \
new -s get -n emacs \; \
send-keys 'get.emacs' C-m \; \
neww -n git \; \
send-keys 'get.git' C-m \; \
neww -n mini \; \
send-keys 'get.mini' C-m \; \
neww -n port \; \
send-keys 'get.port' C-m \; \
neww -n rakudo \; \
send-keys 'get.rakudo' C-m \; \
neww -n neil \; \
send-keys 'get.neil && get.neil2 && exit' C-m \; \
neww -n red \; \
send-keys 'get.red && exit' C-m \; \
neww -n cpan \; \
send-keys 'get.cpan && exit' C-m \; \
selectw -t emacs
Moving good comment of #Jo So to a separate answer.
This command drops to shell once the first ls -la finishes:
tmux new-window "/bin/sh -c 'ls -la; exec bash'"
If you intend to quit the process in the new window normally (as in, proper exit), then tmux new-window "vim ; exec bash" would mostly be fine.
However, if you would intend to quit the new process via system interrupt (SIGINT for example). You will have to use trap.
tmux new-window "bash -c 'trap \"bash\" INT; tail -f foo'"