user248237dfsf user248237dfsf - 10 months ago 108
Bash Question

executing shell command in background from script

how can I execute a shell command in the background from within a bash script, if the command is in a string?

For example:

#!/bin/bash
cmd="nohup mycommand";
other_cmd="nohup othercommand";

"$cmd &";
"$othercmd &";


this does not work -- how can I do this?

Answer Source

Leave off the quotes

$cmd &
$othercmd &

eg:

[email protected] /tmp
$ cat test
#!/bin/bash

cmd="ls -la"

$cmd &


[email protected] /tmp
$ ./test

[email protected] /tmp
$ total 6
drwxrwxrwt+ 1 nicholas root    0 2010-09-10 20:44 .
drwxr-xr-x+ 1 nicholas root 4096 2010-09-10 14:40 ..
-rwxrwxrwx  1 nicholas None   35 2010-09-10 20:44 test
-rwxr-xr-x  1 nicholas None   41 2010-09-10 20:43 test~
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download