LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-13-2008, 07:12 PM   #1
SirTristan
Member
 
Registered: Feb 2006
Distribution: GNU/Linux
Posts: 54

Rep: Reputation: 15
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?
 
Old 05-13-2008, 07:20 PM   #2
Sorrofix
LQ Newbie
 
Registered: Apr 2008
Location: Canada
Posts: 6

Rep: Reputation: 0
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'.
 
Old 05-13-2008, 07:52 PM   #3
SlowCoder
Member
 
Registered: Oct 2004
Location: Southeast, U.S.A.
Distribution: Fedora (Desktop), CentOS (Server), Knoppix (Diags)
Posts: 934

Rep: Reputation: 38
the 'at' command also allows you to execute a command at a specified time/date, or a specified amount of time in the future.
 
Old 05-14-2008, 09:59 AM   #4
SirTristan
Member
 
Registered: Feb 2006
Distribution: GNU/Linux
Posts: 54

Original Poster
Rep: Reputation: 15
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?
 
Old 05-14-2008, 08:06 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
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.
 
Old 05-14-2008, 09:13 PM   #6
SirTristan
Member
 
Registered: Feb 2006
Distribution: GNU/Linux
Posts: 54

Original Poster
Rep: Reputation: 15
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.
 
Old 05-15-2008, 01:23 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
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...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem executing mv command inside shell script pablogosse Linux - General 7 12-16-2009 01:21 PM
Executing a command in Shell Script gjagadish Programming 7 10-15-2007 05:23 AM
executing Bourne shell variable as command? ocicat Programming 3 07-31-2007 03:00 AM
Executing a shell script command!! vishamr2000 Programming 4 08-15-2005 01:33 AM
how-to: repeat OR iterate shell OR bash command delay OR interval admarshall Linux - General 5 07-18-2005 11:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration