[SOLVED] How can I run a Cron job manually or how can I sure my job executed?
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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>
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.
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.
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
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
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...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.