I did this: ssh -f user@host &quot;screen -dm -S screenName&quot;  Now I want to add a bash script command to this line so that the command is executed then the screen detaches. edit: so I want to do so ssh -f user@host &quot;screen -dm -S screenName bash bashing.sh&quot;  bashing.sh: #!/bin/bash echo &quot;testing it&quot;;  but actually nothing happens when I run the command over ssh. Even when I just from the server run this command only screen -dm -S screenName bash bashing.sh. edit 1: I tried everything so far in the answers and comments and even in the suggested question as duplicate but nothing works for me, so any help would be still very appreciated. Sincerely
121768
I'd like to do a one line bash command that automatically does this:  screen -S myserver  python myserver.py  # inside the new screen CTRL A, D (detach)   I think this won't work:  screen -S myserver &amp;&amp; python myserver.py   because python myserver.py won't be started inside the screen.
21959
Under unix/linux, there is this extremely useful program screen: it's for bash, text-only, and I can detach a session, log out (the session is still running though), log in later (even from a different computer) and resume the session exactly as I left it.  My question is, is there an equivalent to screen for X?  So what I want to do is: work remotely with ssh -X in an X-session on a remote linux machine, log out, then later log in from a different computer again with ssh -X and then re-attach the X-session; practically resuming work from the moment when I logged out before. Is this possible?
121067
I have a script which can run as sudo script.sh or pkexec script.sh  It would be much nicer from the user point of view if the script asked for the password from the user when just running it by name script.sh.   How can I "embed" request to pkexec or sudo to run the whole script with root privilege?  Note that running everything with sudo sh -c might not be the best solution as I have functions in the script.
95185
A sample script can be as below:  #!/bin/bash sudo su ls /root   When using ./test.sh as the normal user, instead run ls as super user and exit, it switches to root; and when I logout, it executes ls /root as the normal user.  Can anybody tell me about the mechanism about it?
20785
I don’t actually need to run the command below, and I am aware of ssh keys if I did want to log in without typing a password. I would just like to know how ssh seems to circumvent my file redirection.  Running the following command, ssh prompts me for a password:  ssh nobody@192.168.1.151 &gt; /dev/null 2&gt; /dev/null &lt; /dev/null   How does this work? stdin, stdout, and stderr are all redirected, but ssh still manages to print a prompt on my screen and read what I type. I tried adding the ssh arguments -t -t and -T (not both at once), to enable or disable pseudo-tty allocation, but it made no difference.  I also tried closing all input and output this way, although I don’t know if I typed it correctly. Still, ssh prompts for a password:  (exec 0&lt;&amp;- 1&lt;&amp;- 2&lt;&amp;- 0&gt;&amp;- 1&gt;&amp;- 2&gt;&amp;-; ssh nobody@192.168.1.151)
55428
I have a bash script myhome.sh containing only one line:  echo $HOME   The script's owner is a user:  $ ls -l myhome.sh -rw-rw-r-- 1 user user &lt;date&gt; &lt;time&gt; myhome.sh   In Ubuntu 16.04 and 17.10 I get:  $ echo $HOME /home/user  $ sudo echo $HOME /home/user  $ bash myhome.sh /home/user  $ sudo bash myhome.sh /home/user   In Debian Buster/Testing I get:  $ echo $HOME /home/user  $ sudo echo $HOME /home/user  $ bash myhome.sh /home/user  # WHY ? $ sudo bash myhome.sh /root   I don't understand why inside the script in Debian, if it's executed with sudo, I always get $HOME=/root while in Ubuntu I get $HOME=/home/user. Does anyone know what have the Ubuntu developers changed?
79110
I am trying to write a bash script in a file that would, when run start pinging a host until it becomes available, when the host becomes reachable it runs a command and stops executing, I tried writing one but the script continues pinging until the count ends,  Plus I need to put that process in the background but if I run the script with the dollar ($) sign it still runs in foreground,  #!/bin/bash ping -c30 -i3 192.168.137.163 if [ $? -eq 0 ] then /root/scripts/test1.sh exit 0 else echo “fail” fi
71067
I was surprised that I didn't find this question already on the site. So, today $ came up after I logged in as a new user. This was unexpected because my main user's prompt starts with username@computername:~$.  So, how do I switch from this other shell to bash?
81308
I want to run a command such as this in a bash script:  freebcp &lt;authentication and other parameters&gt; -t "," -r "\r\n"   When run directly on the command line, the command works as expected. But when placed in a variable in a bash script it returns an error such as this:  Msg 20104, Level 3 Unexpected EOF encountered in bcp datafile  Msg 20074, Level 11 Attempt to bulk copy an oversized row to the server  bcp copy in failed   When command is placed in a variable and double quotes are escaped:  cmd="freebcp ${db_name}.dbo.${table_name} in ${p_file} -S ${server_name} -U ${username} -P ${password} -t \",\" -r \"\r\n\" -c" `$cmd`   Note: Putting the following in the script works as expected:  `freebcp ${db_name}.dbo.${table_name} in ${p_file} -S ${server_name} -U ${username} -P ${password} -t "," -r "\r\n" -c`   So I know there's some quoting/escaping/expansion problems but I can't figure out how to fix it.  Note 2: Single quoting -t -r parameters doesn't work either
14957
I have the following shortcut which opens an SSH instance. How can I modify it so that when SSH closes, the terminal stays open?  [Desktop Entry] Terminal=true Type=Application Name[en_US]=ssh Icon=/logo_sprite.png Exec=gnome-terminal --geometry=... --window-with-profile=... --title=... -e 'bash -c "ssh -t -i ~/dsa.key cle@12.210.9.17 sudo -s"'
71756
I want to run a simple bash script automatically when I log in. For example  #!/bin/bash echo "start spotify" gnome-terminal -e spotify --title spotify   When I run this command, one gnome-terminal shows up and spotify show up. I also want the gnome-terminal to popup "hidden" in a different virtual desktop. (one of the other four virtual desktops you can choose from taskbar)   I tried to add this to /home/me/.bash_login or something, but that didn't work..
79497
ssh has an annoying feature in that when you run:  ssh user@host cmd and "here's" "one arg"   Instead of running that cmd with its arguments on host, it concatenates that cmd and arguments with spaces and runs a shell on host to interpret the resulting string (I guess that's why its called ssh and not sexec).  Worse, you don't know what shell is going to be used to interpret that string as that's the login shell of user which is not even guaranteed to be Bourne like as there are still people using tcsh as their login shell and fish is on the rise.  Is there a way around that?  Suppose I have a command as a list of arguments stored in a bash array, each of which may contain any sequence of non-null bytes, is there any way to have it executed on host as user in a consistent way regardless of the login shell of that user on host (which we'll assume is one of the major Unix shell families: Bourne, csh, rc/es, fish)?  Another reasonable assumption that I should be able to make is that there be a sh command on host available in $PATH that is Bourne-compatible.  Example:  cmd=(   'printf'   '&lt;%s&gt;\n'   'arg with $and spaces'   '' # empty   $'even\n* * *\nnewlines'   "and 'single quotes'"   '!!' )   I can run it locally with ksh/zsh/bash/yash as:  $ "${cmd[@]}" &lt;arg with $and spaces&gt; &lt;&gt; &lt;even * * * newlines&gt; &lt;and 'single quotes'&gt; &lt;!!&gt;   or  env "${cmd[@]}"   or  xterm -hold -e "${cmd[@]}" ...   How would I run it on host as user over ssh?  ssh user@host "${cmd[@]}"   obviously won't work.  ssh user@host "$(printf ' %q' exec "${cmd[@]}")"   would only work if the login shell of the remote user was the same as the local shell (or understands quoting in the same way as printf %q in the local shell produces it) and runs in the same locale.
8963
Related question:   Answer from there helped me a lot, this command does what I need:  ssh -R 2225:localhost:22 loginOfServerWithPublicIP@publicIP   So I wrote the script to reconnect all the time:   #!/bin/bash   while true; do     echo "try to connect..."     ssh -o ServerAliveInterval=240 -R 2225:localhost:22 user@host     echo "restarting in 5 seconds.."     sleep 5  done   And added it to the /etc/crontab. But I found out that works if only I execute it "by hand" from shell, but if it is called by cron, ssh connects and immediately finishes. (so, the script above reconnects all the time)  From man ssh, I found that for background connections I should call it with -n key, but it didn't help. Then, I just looked around for similar scripts and I found that it works if I call tail -f something, i.e. some "neverending" command, so I just created empty file /tmp/dummy_file and now my ssh command looks like this:  ssh -o ServerAliveInterval=240 -R 2225:localhost:22 -n user@host tail -f /tmp/dummy_file   It works now! But, this solution seems a bit ugly, plus I don't really understand actual reasons of that behavior. Just by chance, I tried to call bash instead of tail -f (bash seems to me "neverending" command, too), but it doesn't work.  So, could anyone please explain this behavior, and what is the correct way to create background ssh connection to keep reverse ssh tunnel up?
122711
Im trying to make a shortcut to login my ssh server:  ssh x.x.x.x   I made the following file:  ssh_home.sh   Made it executable:  sudo chmod +x ./ssh_home   Checked by right clicking properties to check if it was executable and it was..  But whatever I do, when double clicking it, it will launch gedit. I tried "Open With" but no terminal app is shown there. I searched the net but only found a way to link to the file while not integrating it in one "shortcut file"
79347
I want to create a shortcut for a command in terminal. Like for a command  "ssh user@123.45.7.123", I just want a command "user" and the above command will run. Is this possible, and if so how can it be done?  Thank you!
83139
I wish to use shell to invoke a script on a remote server. I would like to capture the output of that script (its logging messages) and the exit code it returns.   If I do this:  ssh user@server /usr/local/scripts/test_ping.sh echo "$?"   I get the exit code but can't capture the remote logging messages .  If I do this:  local RESULTS=$(ssh user@server /usr/local/scripts/test_ping.sh) echo "$?"  LOG "${RESULTS}";   I get to log my output using my LOG function but can't seem to get a correct exit code, I assume the code I get is the code from the varianble assignment.   I would like to continue to use my LOG function to capture all output as it formats and sends things to a file, syslog, and the screen for me.   How can I capture results in a var AND get the correct exit code from the remote script?
82486
I just want to have a quick overview of my detached GNU screen sessions. Is it possible to dump the current content to stdout ? Something like  screen --print SESSION_NAME &gt; file.txt