LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Executing a command from shell, but with a delay? (https://www.linuxquestions.org/questions/linux-newbie-8/executing-a-command-from-shell-but-with-a-delay-641998/)

SirTristan 05-13-2008 06:12 PM

Executing a command from shell, but with a delay?
 
Is there a way to execute a command, but with a specified time delay? I know this can be done with cron but I was wondering if it was possible from the command line. I thought perhaps there was some way to do this via multiple commands per line, e.g. for the command 'rm test.txt':
Code:

[~]# <delay command>; rm test.txt
Or perhaps there is some other way to do this from the command line?

Sorrofix 05-13-2008 06:20 PM

I think the command you're looking for is 'sleep'. E.g.
Code:

$ sleep 4; rm test.txt
That would sleep for 4 seconds before executing 'rm'.

SlowCoder 05-13-2008 06:52 PM

the 'at' command also allows you to execute a command at a specified time/date, or a specified amount of time in the future.

SirTristan 05-14-2008 08:59 AM

Thanks, sleep and at are what I was looking for.

For sleep, is there any way multiple commands per line can be executed in the background? For instance, the following gives the error:
Code:

# sleep 5 &; rm -f test.php &
-bash: syntax error near unexpected token `;'

Removing the '&' after sleep causes it to work, but then the sleep isn't in the background.

It took me a while to find the 'at' syntax, apparently you have to do one of the following for 'rm -f test.php':
Code:

# echo "rm -f test.php" | at HH:MM
OR: # at HH:MM -f filename

Where filename contains the command "rm -f test.php".

Also for 'at', my atd daemon wasn't started; '/sbin/service atd status' said 'atd is stopped', and trying to use at gave the error 'Can't open /var/run/atd.pid to signal atd. No atd running?'. I had to start the atd daemon with:
Code:

/usr/sbin/atd
Which I also added to /etc/rc.local so it would be done at startup. Would it be better to place this command in .bashrc instead of /etc/rc.local? Is there any reason to use one instead of the other - /etc/rc.local or .bashrc?

chrism01 05-14-2008 07:06 PM

1. the way to make a group of cmds execute in the background is to put them in a separate file and call that, where the 1st line is the normal sleep cmd.

2. /etc/rc.local is really for the global env (run by root at startup). .bashrc is on a per user basis (as that's where the file is).
If you always want atd running, there should be a proper set of stop/start scripts already in /etc/rc.d.
Try the chkconfig cmd or there is usually a Services option on the menu, where you can say which runlevels you want it active for.

SirTristan 05-14-2008 08:13 PM

Thanks. There is an 'atd' in the /etc/rc.d/init.d directory, but it wasn't running at startup until I placed /usr/sbin/atd in /etc/rc.local. Not sure what's going on there.

chrism01 05-15-2008 12:23 AM

That's because you're not supposed to put it in /etc/rc.local. Use menu option or chkconfig to make it start itself properly, see the dirs under /etc/rc.d

Ps please post your distro/ver in your profile: GNU/Linux is just a generic name...


All times are GMT -5. The time now is 01:43 AM.