LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Can I execute multiple sequential commands in a cron using a semi-colon? (https://www.linuxquestions.org/questions/linux-newbie-8/can-i-execute-multiple-sequential-commands-in-a-cron-using-a-semi-colon-4175726350/)

wh33t 06-26-2023 09:58 AM

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

michaelk 06-26-2023 10:05 AM

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

pan64 06-26-2023 10:05 AM

did you try it already?
I would recommend you to write a shell script and put everything into it.

wh33t 06-26-2023 10:11 AM

Quote:

Originally Posted by michaelk (Post 6438658)
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 (Post 6438660)
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.

pan64 06-26-2023 10:18 AM

time command is your friend.

MadeInGermany 06-26-2023 02:38 PM

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


All times are GMT -5. The time now is 06:15 PM.