- remove the ampersand, enter your password and then pause the task with Ctrl+Z and then run
bg
so the job resumes running in the background ; - Dirty workaround: do a dummy
sudo
before (likesudo ls
), then launch yournohup
command, and it won’t ask for your password again and will run anyway.
Workaround with sudo with nohup $ sudo date # Date executes and the password is cached for a few minutes. $ nohup sudo some_script & # Executes without asking for a password, since it's cached.
Commands can produce output on stdout or stderr. The commands that you tried redirected only stdout. Under bash
, you can redirect the output from both streams at once using:
command &>test.txt &
Or
command &>/dev/null &
If you are using a POSIX shell, then you need to do the redirection in steps:
command >test.txt 2>&1 &
>test.txt
redirects stdout to the file test.txt
. Because stderr is file handle 2
, the effect of 2>&1
is to redirect stderr to wherever stdout, denoted by &1
is currently going.
nohup ./my-shell-script.sh >test.log 2>&1 &
1. Execute a command in the background using &
You can execute a command (or shell script) as a background job by appending an ampersand to the command as shown below.
$ ./my-shell-script.sh &
2. Execute a command in the background using nohup
After you execute a command (or shell script) in the background using &, if you logout from the session, the command will get killed. To avoid that, you should use nohup as shown below.
$ nohup ./my-shell-script.sh &
3. Execute a command using screen command
After you execute a command in the background using nohup and &, the command will get executed even after you logout. But, you cannot connect to the same session again to see exactly what is happening on the screen. To do that, you should use screen command.
Linux screen command offers the ability to detach a session that is running some process, and then attach it at a later time. When you reattach the session later, your terminals will be there exactly in the way you left them earlier.
4. Executing a command as a batch job using at
Using at command you can schedule a job to run at a particular date and time. For example, to execute the backup script at 10 a.m tomorrow, do the following.
$ at -f backup.sh 10 am tomorrow
5. Execute a command continuously using watch
To execute a command continuously at a certain interval, use watch command as shown below.
watch df -h