LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 06-26-2023, 09:58 AM   #1
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 922

Rep: Reputation: 61
Can I execute multiple sequential commands in a cron using a semi-colon?


Is that how it works?

Example

Code:
*/15 * * * * date > log.file; apt update >> log.file; date >> log.file
Every 15 minutes

1. print the date and pipe output to create/overwrite log.file
2. update apt and pipe append output to log.file
3. print the date and pipe append output to log.file (this lets me know how long the apt update command took to finish
 
Old 06-26-2023, 10:05 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,759

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
Yes, that should work but it seems silly to run an update every 15 minutes. I guess it depends on what you are actually trying to accomplish. Since the file is being overwritten every time you can not determine if it changes. You can also write the output of the time command to a file to see how long apt takes to update i..e

time apt update > log.file

Last edited by michaelk; 06-26-2023 at 10:07 AM.
 
1 members found this post helpful.
Old 06-26-2023, 10:05 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,966

Rep: Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332
did you try it already?
I would recommend you to write a shell script and put everything into it.
 
1 members found this post helpful.
Old 06-26-2023, 10:11 AM   #4
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 922

Original Poster
Rep: Reputation: 61
Quote:
Originally Posted by michaelk View Post
Yes, that should work but it seems silly to run an update every 15 minutes. I guess it depends on what you are actually trying to accomplish. Since the file is being overwritten every time you can not determine if it changes. You can also write the output of the time command to a file to see how long apt takes to update i..e

time apt update > log.file
Oh yes, the actual command rysnc's a very large directory over the network. I am trying to figure out roughly how long this rsync takes.

I only ever want to know how long and when the last rsync occurred.

Quote:
Originally Posted by pan64 View Post
did you try it already?
I would recommend you to write a shell script and put everything into it.
Duh, of course lol. I'm hesitant to dive into the rabbit hole of shell scripting but I guess something like this is actually pretty simple isn't it.
 
Old 06-26-2023, 10:18 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,966

Rep: Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332
time command is your friend.
 
1 members found this post helpful.
Old 06-26-2023, 02:38 PM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,808

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
The last crontab field accepts a complete standard shell (no bashism), except that a literal % must be escaped \%

The shell allows to group commands for a common redirection:
Code:
*/15 * * * * { date; apt update; date; } > log.file
The following allows the apt to run up to twice the interval time (2*15=30 minutes):
Code:
*/15 * * * * ld=/tmp/mylockdir; if mkdir $ld 2>/dev/null; then apt update; fi; rmdir $ld

Last edited by MadeInGermany; 06-26-2023 at 02:40 PM.
 
  


Reply



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
[SOLVED] sequential : how to find the missing numbers within a sequence of files that have sequential numbers attached to them? BW-userx Programming 71 07-15-2017 08:52 PM
Need to remove / (slash) and replace ; (semi-colon) with new line rakesh.bharad Linux - Newbie 2 05-06-2011 02:01 AM
[SOLVED] expect -- semi-colon in password being translated. bartonski Linux - Software 8 09-13-2009 09:04 AM
shell script using /etc/cron.hourly to execute cron.php file? rioguia Programming 3 06-11-2008 08:09 AM
need a semi colon .. but vert spooge Linux - General 4 09-19-2004 03:52 PM

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

All times are GMT -5. The time now is 12:31 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
Open Source Consulting | Domain Registration