I'm using reptyr to bring a background process to a new terminal.
The description says reptyr should recognize ctrl+z, but it doesn't.
I'm wondering how I can set the process I just brought to the terminal, back to the background.
What I had in mind was:
reptyr <processid>
(ctrl+z)
bg
disown
on the other console you can run:
nohup ./reptyr $PID_OF_PREVOUS_REPTYL_COMMAND &> /dev/null &
$PID_OF_PREVOUS_REPTYL_COMMAND you can retrieve from ps fx command
Related
I want to use shell script to launch Redis server and then monitor a log file:
#!/bin/bash
/path/to/redis/src/redis-server &
tail -f /path/to/log/logfile.log
If I run this script and press Ctrl+C from the terminal, the tail -f terminated, which is what I want, however the Redis also detected SIGINT and exited.
I tried to write the script like this:
#!/bin/bash
trap '' INT TSTP
~/redis/src/redis-server &
tail -f ./script1
This time things go even worse, the tail -f refused to terminate while Redis still detected SIGINT and exited.
It seems that there is some problems specific to Redis regarding ignoring signals.
My goal is to make tail -f responds to Ctrl+C while making Redis ignore this signal.
Please anyone tell me whether this can be achieved and if so, give me some advice?
redis-server catches SIGINT (Ctrl+C), even if SIGINT was being ignored. This is an unusual choice; most software will check and won't catch SIGINT if it's already being ignored.
When it receives SIGINT, it saves the database and shuts down.
If you start it as a service, it won't be associated with any terminal at all, and won't see any Ctrl+C you type.
If you start it as a background job in an interactive shell:
$ /path/to/redis/src/redis-server &
your shell will put it into a process group that is different from the terminal's process group, and typing Ctrl+C won't affect it. (If you bring it to the foreground with fg, Ctrl+C will send SIGINT to the program).
But, when you run a script like this:
#!/bin/bash
/path/to/redis/src/redis-server &
tail -f /path/to/log/logfile.log
the shell that runs the script will be non-interactive, and any program that it starts in the background (with &) will be in the same process group as the shell. So if you run that shell script in the foreground, typing Ctrl+C will send SIGINT to the shell, to redis-server, and to tail.
To prevent Ctrl+C from sending SIGINT to redis-server in a case like this, you need to either put redis-server in its own process group or disassociate it from your terminal. You can do this with setsid, which does both:
#!/bin/bash
setsid /path/to/redis/src/redis-server &
tail -f /path/to/log/logfile.log
I see a lot of people are using the following command:
nohup ./sendData.sh >logs/send.log 2>&1 &
Since the nohup has already been used, do I still need to add & on the trail?
nohup tells the terminal to not hang up, even when the parent terminal is closed. & tells the terminal to send the process to the background. So
command &
will send the command to the background. You can then use the shell to invoke other commands. If the terminal is exited, the jobs are killed.
nohup command
will not send the command to the background, but when you close the terminal, the process will live on until it is finished or killed.
nohup command &
will send the command to the background and the process will not be killed even when you close the terminal. Usually when using nohup, one also wants the process to be in the background, that's why you see the combination of nohup and & more often than not.
In a shell program I want to launch a program and get its PID and save in a temp file. But here I will launch the program in the foreground and will not exit the shell until the process is in running state
ex:
#!/bin/bash
myprogram &
echo "$!" > /tmp/pid
And this works fine i am able to get the pid of the launched process . But if i launch the program in fore ground i want to know how to get the pid
ex :
#!/bin/bash
myprogram /// hear some how i wan to know the PID before going to next line
As I commented above since your command is still running in foreground you cannot enter a new command in the same shell and goto the next line.
However while this command is running and you want to get the process id of this program from a different shell tab/window process then use pgrep like this:
pgrep -f "myprogram"
17113 # this # will be different for you :P
EDIT: Base on your comment or is it possible to launch the program in background and get the process ID and then wait the script till that process gets exited ?
Yes that can be done using wait pid command as follows:
myprogram &
mypid=$!
# do some other stuff and then
wait $mypid
You can't do this since your shell script isn't running -- the command you just launched in the foreground is.
I use this to run rsync in background
rsync -avh /home/abc/abac /backups/ddd &
When i do that i see line saying 1 process stopped.
Now does that mean my process is still running ot it is stopped
When you press "ctrl + z" then the process stopped and go to background.
[1]+ Stopped rsync -ar --partial /home/webup/ /mnt/backup/
Now press "bg" and will start in background the previous process you stopped.
[1]+ rsync -ar --partial /home/webup/ /mnt/backup/ &
Press "jobs" to see the process is running
[1]+ Running rsync -ar --partial /home/webup/ /mnt/backup/ &
If you want to to go in foreground press "fg 1" 1 is the process number
The solution to keep rsync running in background
nohup rsync -a host.origin:/path/data destiny.host:/path/ &
Nohup allows to run a process/command or shell script to continue working in the background even if you close the terminal session.
In our example, we also added ‘&’ at the end, that helps to send the process to background.
Output example:
nohup rsync -avp root#61.0.172.109:/root/backup/uploads/ . &
[1] 33376
nohup: ignoring input and appending output to 'nohup.out'
That’s all, now your rsync process will run in the background, no matter what happens it will be there unless you kill the process from command line, but it will not be interrupted if you close your linux terminal, or if you logout from the server.
RSync Status
cat nohup.out
It is probably trying to read from the terminal (to ask you for a password, perhaps). When a background process tries to read from the terminal, it gets stopped.
You can make the error go away by redirecting stdin from /dev/null:
rsync -avh /home/abc/abac /backups/ddd < /dev/null &
...but then it will probably fail because it will not be able to read whatever it was trying to read.
No, it means it has been stopped.
You can check it with jobs.
Example output:
jobs
[1]+ Stopped yes
Then you can reactivate with fg, example:
fg 1
This is safe, you can monitor nohup.out to see the progress.
nohup rsync -avrt --exclude 'i386*' --exclude 'debug' rsync://mirrors.kernel.org/centos/6/os . &
If everything works, your call should return you the PID of the new progress and some time later a "Done" message.
So yeah, your output looks like your process is not running.
Check ps to see if rsync is running.
File Transfer:
nohup scp oracle#<your_ip>:/backup_location/backup/file.txt . > nohup.out 2>&1 &
then hit ctrl-z
$ bg
To bring the command alive
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Prevent a background process from being stopped after closing SSH client
I have a program that takes a lot of time to finish.
It is running as root over ssh.
I want it to continue to run after I logout,is this possible and how would I achieve this?
Assuming that you have a program running in the foreground, press ctrl-Z, then:
[1]+ Stopped myprogram
$ disown -h %1
$ bg 1
[1]+ myprogram &
$ logout
If there is only one job, then you don't need to specify the job number. Just use disown -h and bg.
Explanation of the above steps:
You press ctrl-Z. The system suspends the running program, displays a job number and a "Stopped" message and returns you to a bash prompt.
You type the disown -h %1 command (here, I've used a 1, but you'd use the job number that was displayed in the Stopped message) which marks the job so it ignores the SIGHUP signal (it will not be stopped by logging out).
Next, type the bg command using the same job number; this resumes the running of the program in the background and a message is displayed confirming that.
You can now log out and it will continue running..
You should try using nohup and running it in the background:
nohup sleep 3600 &
I would try the program screen.
Start in the background:
./long_running_process options &
And disown the job before you log out:
disown
You want nohup. See http://nixcraft.com/linux-software/313-ssh-nohup-connection.html
You could use screen, detach and reattach