The following command : display=:0.0 in bash script is used. Then when calling export, I can see it's there.
However, after a while, this command is no more in the export list and I have to do it again.
Note
There is only 1 session running and I'm doing nothing on the command line. There may be a program running in background.
This is a ssh session (putty).
What could be causing this?
I was logged with ssh. I realised I sometime got logged off and on automatically. Then the export command would be off because it's a "new" session. Cannot explain why this happen with putty though.
Related
I've just switched to fish-shell
And I've used the instructions of How do I run a command every login? What's fish's equivalent to .bashrc?
Which means I've moved the commands which i prefer to run upon login from .bashrc to ~/.config/fish/config.fish
But right now the commands keep running if i open screen or tmux session ! but before while i were using the default shell that's was never happens (meant that the commands were only run during the login and never re-run in screen session)
How to avoid this?
Thanks in advance.
You can test for the TERM environmental variable to see if your shell is running in such a session. Both screen and tmux by default set it to 'screen'.
if not string match --quiet -e $TERM 'screen'
<your startup scripts>
end
Note that other useful indicators are whether a shell is interactive or a login shell. You can use status --is-interactive and status --is-login to check for these two states.
In your specific case, a check for login shell might be what you are looking for:
if status --is-login
<your startup scripts>
end
See https://unix.stackexchange.com/questions/38175/difference-between-login-shell-and-non-login-shell for an explanation.
What am I trying to achieve?
I'm trying to get crontab to kill the previous tmux session and create a new tmux session (with particular teamocil settings).
Simple bash script that the crontab runs:
#!/bin/bash
tmux kill-session;
tmux new-session -d "source /home/qa/.bash_profile;teamocil settings;";
Issue I'm having?
Running this script manually works fine, but when running through crontab it will only work if at least 2 other tmux sessions pre-exist, i.e. it kills a session as part of the script, if there are then no sessions left the crontab won't create the 1st session. If after killing a session there is still another session available, then the script works.
Findings so far?
I've found that if I declare the source as part of the bash script, not in the tmux new sessions command, then it works fine. Why would this be? See modified script below that works:
#!/bin/bash
source /home/qa/.bash_profile
tmux kill-session;
tmux new-session -d "teamocil settings;";
It would be really helpful to understand why this made a difference, to help me update other scripts and not make this mistake again. Any light that can be shed on this is appreciated.
Put path to profile before path to script directly in crontab line, this will make script more flexible for future usage.
Explanation https://serverfault.com/questions/337631/crontab-execution-doesnt-have-the-same-environment-variables-as-executing-user
Very good answer https://unix.stackexchange.com/questions/27289/how-can-i-run-a-cron-command-with-existing-environmental-variables
another very good answer https://unix.stackexchange.com/questions/6790/executing-a-sh-script-from-the-cron
Recomendation:
There is probably answer to you question somewhere. One needs just to search for it.
I have an issue about how to run screen through crontab, I made a script which check if a previous session is running and if not restart a screen.
cf screenshots below
crontab
restard_td.sh
When I run the script manually it's works fine, by cron, i can see my output "down, i will restart it " in my log file but no screen available and my script doesn't run.
Has someone got an idea ?
you can try checking the cron log, and use the screen full path.
I fixed it by using screen full path and python full path in my script.
like that
/usr/bin/screen -dmS team /usr/local/bin/python /home/admin/scripts/rattrapage/team.py
I am using elastic map reduce from Amazon. I am sshing into hadoop master node and executing a script like.
$EMR_BIN/elastic-mapreduce --jobflow $JOBFLOW --ssh < hivescript.sh . It sshes me into the master node and runs the hive script. The hivescript contains the following lines
hive
add jar joda-time-1.6.jar;
add jar EmrHiveUtils-1.2.jar;
and some commands to create hive tables. The script runs fine and creates the hive tables and everything else, but comes back to the prompt from where I ran the script. How do I leave it sshed into hadoop master node at the hive prompt.
Consider using Expect, then you could do something along these lines and interact at the end:
/usr/bin/expect <<EOF
spawn ssh ... YourHost
expect "password"
send "password\n"
send javastuff
interact
EOF
These are the most common answers I've seen (with the drawbacks I ran into with them):
Use expect
This is probably the most well rounded solution for most people
I cannot control whether expect is installed in my target environments
Just to try this out anyway, I put together a simple expect script to ssh to a remote machine, send a simple command, and turn control over to the user. There was a long delay before the prompt showed up, and after fiddling with it with little success I decided to move on for the time being.
Eventually I came back to this as the final solution after realizing I had violated one of the 3 virtues of a good programmer -- false impatience.
Use screen / tmux to start the shell, then inject commands from an external process.
This works ok, but if the terminal window dies it leaves a screen/tmux instance hanging around. I could certainly try to come up with a way to just re-attach to prior instances or kill them; screen (and probably tmux) can make it die instead of auto-detaching, but I didn't fiddle with it.
If using gnome-terminal, use its -x or --command flag (I'm guessing xterm and others have similar options)
I'll go into more detail on problems I had with this on #4
Make a bash script with #!/bin/bash --init-file as the shebang; this will cause your script to execute, then leave an interactive shell running afterward
This and #3 had issues with some programs that required user interaction before the shell is presented to them. Some programs (like ssh) it worked fine with, others (telnet, vxsim) presented a prompt but no text was passed along to the program; only ctrl characters like ^C.
Do something like this: xterm -e 'commands; here; exec bash'. This will cause it to create an interactive shell after your commands execute.
This is fine as long as the user doesn't attempt to interrupt with ^C before the last command executes.
Currently, the only thing I've found that gives me the behavior I need is to use cmdtool from the OpenWin project.
/usr/openwin/bin/cmdtool -I 'commands; here'
# or
/usr/openwin/bin/cmdtool -I 'commands; here' /bin/bash --norc
The resulting terminal injects the list of commands passed with -I to the program executed (no parms means default shell), so those commands show up in that shell's history.
What I don't like is that the terminal cmdtool provides feels so clunky ... but alas.
I do a fair bit of work at the command line. When I start my computer up on of the first things I do is open up a terminal window for mysql, and one for the Rails console and usually a third running mongrel. Setting it up every morning is a bit of a drag so I would like to script it. How can I open a terminal window, log into mysql, select my development database and then leave it there at the mysql prompt waiting for me. I know how to execute a mysql statement from bash, I just don't know how to get it to leave the prompt open for me to work with after.
Hopefully that is clear!
Update:
Combining the two answers below got things working for mysql. Thanks!
Now I am trying to get a gnome-terminal window to stay open running the Rails script/server command so I can watch the output. For some reason the following closes almost immediately:
gnome-terminal -e "ruby /home/mike/projects/myapp/script/server" &
xterm provides an option for executing a command:
xterm -e myCommandToLogIntoMysql &
You can put a sequence of such xterm commands into a shell script.
How can I open a terminal window, log
into mysql, select my development
database and then leave it there at
the mysql prompt waiting for me.
mysql -u user -ppassword -D database_name
Remember not to put space between "-p" and password. Note - this is a bit insecure, as your password is visible in process list so anyone can read it using ps. You can, however, put your MySQL password in ~/.my.cnf file.