LinuxQuestions.org
Help answer threads with 0 replies.
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 02-13-2021, 09:01 AM   #31
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936

You are not paying attention to the syntax.

Code:
00 23 * * 1 /usr/bin/App1 && /usr/bin/echo "App1 done: $(/usr/bin/date +"\%Y-\%m-\%d")"  >> /var/log/CronApp.log
There can not be a space between the + sign and "\%Y-\%m-\%d". The % must be escaped with a backslash.

Last edited by michaelk; 02-13-2021 at 09:02 AM.
 
1 members found this post helpful.
Old 02-13-2021, 01:51 PM   #32
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
Code:
/usr/bin/echo "App1 done: $(/usr/bin/date + "%Y-%m-%d" )"
should be
Code:
/usr/bin/echo "App1 done: $(/usr/bin/date +"%Y-%m-%d" )"
gives
App1 done: 2021-02-13
Your space between the + and "%Y-%m-%d" causes the error.

The \ is not required in bash for date formatting.
In fact, putting in the \ gives this
Code:
$ /usr/bin/echo "App1 done: $(/usr/bin/date +"\%Y-\%m-\%d" )"
App1 done: \2021-\02-\13

Last edited by computersavvy; 02-13-2021 at 01:56 PM.
 
1 members found this post helpful.
Old 02-13-2021, 02:24 PM   #33
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936
Runing date from cron is different then the CLI. From the cron man page.

Quote:
The "sixth" field (the rest of the line) specifies the command to
be run. The entire command portion of the line, up to a newline
or a "%" character, will be executed by /bin/sh or by the shell
specified in the SHELL variable of the cronfile. A "%" character
in the command, unless escaped with a backslash (\), will be
changed into newline characters, and all data after the first %
will be sent to the command as standard input.
This is true for vixie and other versions of cron based on vixie but I don't think this is true for all versions. However, most distributions that I run use vixie so it is habit.

Last edited by michaelk; 02-13-2021 at 03:09 PM.
 
Old 02-13-2021, 03:46 PM   #34
Brains
Senior Member
 
Registered: Apr 2009
Distribution: All OS except Apple
Posts: 1,591

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
In the past, I would use /etc/crontab, or starup applications to run the script to mount shared folders in a Linux virtual machine. They were inconsistent, works in this distribution but not that one. Anyway, since I only run systemd init OSs, now I just create units to run scripts. Very easy actually, below shows a quick way to get the lowdown on my mount shared folders unit:
Quote:
jo@willy ~ $ systemctl status vmwareshare
● vmwareshare.service - Mount-Shared-Data
Loaded: loaded (/etc/systemd/system/vmwareshare.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Sat 2021-02-13 07:32:08 MST; 7h ago
Process: 415 ExecStart=/bin/bash /home/jo/Documents/shares.sh (code=exited, status=0/SUCCESS)
Main PID: 415 (code=exited, status=0/SUCCESS)
CPU: 167ms
CGroup: /system.slice/vmwareshare.service
└─483 /usr/bin/vmhgfs-fuse .host:/Data /home/jo/Data -o subtype=vmhgfs-fuse,allow_other

Feb 13 07:32:05 willy systemd[1]: Started Mount-Shared-Data.
Feb 13 07:32:08 willy systemd[1]: vmwareshare.service: Succeeded.
Feb 13 07:32:08 willy systemd[1]: vmwareshare.service: Unit process 483 (vmhgfs-fuse) remains running after unit stoppe>
 
Old 02-19-2021, 12:04 AM   #35
n00b_noob
Member
 
Registered: Sep 2020
Posts: 436

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by computersavvy View Post
Code:
/usr/bin/echo "App1 done: $(/usr/bin/date + "%Y-%m-%d" )"
should be
Code:
/usr/bin/echo "App1 done: $(/usr/bin/date +"%Y-%m-%d" )"
gives
App1 done: 2021-02-13
Your space between the + and "%Y-%m-%d" causes the error.

The \ is not required in bash for date formatting.
In fact, putting in the \ gives this
Code:
$ /usr/bin/echo "App1 done: $(/usr/bin/date +"\%Y-\%m-\%d" )"
App1 done: \2021-\02-\13
Thank you.
App1 is OK, but another one is not OK. I used below line for another cron job:
Code:
00 23 * * * /usr/local/bin/wp core update --path=/var/www/wp/ && /usr/local/bin/wp plugin update --all --path=/var/www/wp && /usr/bin/echo "WordPress Update done: $(/usr/bin/date +"\%Y-\%m-\%d")" >> /var/log/CronApp2.log
Why? Is this because of more than one command?

Last edited by n00b_noob; 02-19-2021 at 12:05 AM.
 
Old 02-19-2021, 05:23 AM   #36
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936
You will only see an update done message in the log if all commands ran successfully.
 
Old 02-19-2021, 12:36 PM   #37
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
Quote:
Originally Posted by michaelk View Post
You will only see an update done message in the log if all commands ran successfully.
Depending on the command, yes, but if you use something like I posted in #26 above it will give either a success or a failure message into the log
 
Old 02-21-2021, 05:37 AM   #38
n00b_noob
Member
 
Registered: Sep 2020
Posts: 436

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by computersavvy View Post
Depending on the command, yes, but if you use something like I posted in #26 above it will give either a success or a failure message into the log
Thank you.
The "CronApp2.log" file never created and that means that cron job not executed properly. Why?
 
Old 02-21-2021, 10:02 PM   #39
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
Quote:
Originally Posted by n00b_noob View Post
Thank you.
The "CronApp2.log" file never created and that means that cron job not executed properly. Why?
Does not mean it did not happen.
The log entry the way you created it will only happen if the entire command line is successfully completed.

Is the cron job running as root?
What happens if you run the exact same command in its entirety on the command line?

If you are referring to this cron command you posted some time back
Code:
/usr/local/bin/wp core update --path=/var/www/wp/ && /usr/local/bin/wp plugin update --all --path=/var/www/wp && /usr/bin/echo "WordPress Update done: $(/usr/bin/date +"\%Y-\%m-\%d")" >> /var/log/CronApp2.log
then I would say it likely that either the core update or the plugin update failed since it would only get to the echo command if both the previous commands completed successfully.

Please practice analyzing your commands so you understand what they do and what happens if they fail. That cron command you posted has 3 parts and if anything fails then what follows never gets executed.
 
Old 02-23-2021, 11:31 AM   #40
n00b_noob
Member
 
Registered: Sep 2020
Posts: 436

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by computersavvy View Post
Does not mean it did not happen.
The log entry the way you created it will only happen if the entire command line is successfully completed.

Is the cron job running as root?
What happens if you run the exact same command in its entirety on the command line?

If you are referring to this cron command you posted some time back
Code:
/usr/local/bin/wp core update --path=/var/www/wp/ && /usr/local/bin/wp plugin update --all --path=/var/www/wp && /usr/bin/echo "WordPress Update done: $(/usr/bin/date +"\%Y-\%m-\%d")" >> /var/log/CronApp2.log
then I would say it likely that either the core update or the plugin update failed since it would only get to the echo command if both the previous commands completed successfully.

Please practice analyzing your commands so you understand what they do and what happens if they fail. That cron command you posted has 3 parts and if anything fails then what follows never gets executed.
The output of command is:
Code:
# /usr/local/bin/wp core update --path=/var/www/wp/ && /usr/local/bin/wp plugin update --all --path=/var/www/wp
Success: WordPress is up to date.
Success: Plugin already updated.
 
Old 02-23-2021, 01:36 PM   #41
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
But you did not run it verbatim to the cron command.
What you just posted did not include the echo out to the log file.
As a result you have no idea if the echo command would be executed.
The core update succeeded, the plugin update gave a different response (already updated) and did not actually complete the update. Would that cause the echo to occur or not? I suspect not since it did not say succeeded.

Remember, with the "commandA && commandB && commandC" structure
CommandB only gets executed if commandA reports success. CommandC only gets executed if commandA & commandB both report success.

Also, you did not say what user ran that command. The user running the command (which for the cron job I assume is root) must be able to write to the log file (and the directory where it is located). Root can write to /var/log but no one else can in most cases.

I really suspect that you should write a script that executes each of those commands, one at a time, and saves the return value of each. Then after the commands have returned their status the output can be sent to the log showing the exact status of each.

something similar to this might be a starting point.
Code:
#!/usr/bin/bash

# file:  wpupdate.sh

# first set variables and paths

CMD=/usr/local/bin/wp
Path=/usr/www/wp
Date="$(/usr/bin/date +"%Y-%M-%D")"
CoreResult=""
PluginResult=""
LogFile=/var/log/App2.log

# now run the updates

CoreResult="$($CMD core update --path=$Path)"
PluginResult="$($CMD plugin update --all --path=$Path)"

# now send the results to the log
echo "App2 ran at $Date with results:" >> $LogFile
echo "$CoreResult" >> $LogFile
echo "$PluginResult" >> $LogFile
1. Save this script to a file after making whatever changes you feel necessary.
2. make the file executable
3. change the cron job entry to execute this file


This should produce an entry in the log file regardless of whether an update was successful, not needed, or failed
 
1 members found this post helpful.
Old 02-24-2021, 01:49 PM   #42
n00b_noob
Member
 
Registered: Sep 2020
Posts: 436

Original Poster
Rep: Reputation: Disabled
Sorry, it was my problem. I had an error in the php.ini file and when I fixed it, then anything worked:
Code:
00 23 * * * /usr/local/bin/wp core update --path=/var/www/wp/ && /usr/local/bin/wp plugin update --all --path=/var/www/wp && /usr/bin/echo "WordPress Update done: $(/usr/bin/date +"\%Y-\%m-\%d")" >> /var/log/CronApp2.log
 
Old 02-24-2021, 01:52 PM   #43
n00b_noob
Member
 
Registered: Sep 2020
Posts: 436

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by computersavvy View Post
But you did not run it verbatim to the cron command.
What you just posted did not include the echo out to the log file.
As a result you have no idea if the echo command would be executed.
The core update succeeded, the plugin update gave a different response (already updated) and did not actually complete the update. Would that cause the echo to occur or not? I suspect not since it did not say succeeded.

Remember, with the "commandA && commandB && commandC" structure
CommandB only gets executed if commandA reports success. CommandC only gets executed if commandA & commandB both report success.

Also, you did not say what user ran that command. The user running the command (which for the cron job I assume is root) must be able to write to the log file (and the directory where it is located). Root can write to /var/log but no one else can in most cases.

I really suspect that you should write a script that executes each of those commands, one at a time, and saves the return value of each. Then after the commands have returned their status the output can be sent to the log showing the exact status of each.

something similar to this might be a starting point.
Code:
#!/usr/bin/bash

# file:  wpupdate.sh

# first set variables and paths

CMD=/usr/local/bin/wp
Path=/usr/www/wp
Date="$(/usr/bin/date +"%Y-%M-%D")"
CoreResult=""
PluginResult=""
LogFile=/var/log/App2.log

# now run the updates

CoreResult="$($CMD core update --path=$Path)"
PluginResult="$($CMD plugin update --all --path=$Path)"

# now send the results to the log
echo "App2 ran at $Date with results:" >> $LogFile
echo "$CoreResult" >> $LogFile
echo "$PluginResult" >> $LogFile
1. Save this script to a file after making whatever changes you feel necessary.
2. make the file executable
3. change the cron job entry to execute this file


This should produce an entry in the log file regardless of whether an update was successful, not needed, or failed
Thank you for your time and great answer.
 
Old 02-24-2021, 10:38 PM   #44
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,369

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
Definitely recommend the above ie if >1 cmd, write script instead.
In fact I usually do that anyway, because it usually expands or I want to do extra handling for eg errors...
 
  


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
Debian daily cron job won't run, but does run in cron.hourly. sandersch Linux - General 7 05-24-2012 01:50 AM
[SOLVED] Configured Cron job executed every hour is instead executed every minute for 10m markings Linux - Software 4 05-13-2012 05:43 PM
[SOLVED] Xwindow's program will not run when executed on boot or when executed remotely richman1234 Programming 2 10-08-2010 01:32 PM
how can we know whether a Cron job has been executed or not ?? vikas027 Linux - Server 6 04-17-2009 01:49 PM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM

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

All times are GMT -5. The time now is 07:10 AM.

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