Wordpress Cron: Running as which user? - cron

I'm writing a plugin. It has some job to be execute by wp_cron. Now within the script, it create/edit some posts automatically. So which user will be running the cron job?
Also I notice it failed when calling WP_Filesystem()

Well I've found out that it's running as no one, as get_current_user_id() returns 0

I've been doing some debugging today and I've found that get_current_user_id() is often, but not always, 0 (logged out). The cron can get triggered by any user and your code should anticipate that.

Related

When is crontab executed?

I have several crontab jobs that are executed every minute and I’m thinking on putting “#reboot” jobs. They are put and executed with root privileges.
So here’s what I want to know. Will these cronjobs run when system gets to login screen after rebooting? Will “#reboot” entries run after reboot without me getting logged into root?
Yes, they should run automatically as soon as the cron service gets started. The exact moment can vary depending on the cron implementation and your init system.

Ways to schedule long running laravel tasks without root and privileged user

Me and my team are using a shared hosting service with a limited linux container (without root and privileged user) and we need to develop a new feature that involves long running tasks (> 600ms).
We thought of two possible solutions:
Breaking apart the task, and via the frontend, make one separate http request to the server.
Use bash screen to run a bash script with a infinite loop calling php artisan schedule:run (mimicking cronjob)
I don't feel very confortable with the first solution, moving server logic to the browser seens wrong in my opinion.
The second solution is only a supposition (not tested), we are not sure if the bash screen would randomly stop at any time.
What would be the least unstable way to achive our goal? Thx
Assuming you already explored this because you mention that a CRON would not be an option, but even unprivileged users can setup a CRON, which is the simplest solution in combination with the Laravel scheduler.
If an actual CRON using the scheduler is really out of the question I do think making an HTTP endpoint you could call from the browser is the next best thing. Just don't think an endpoint you can call from a browser that you can only call it from a browser ;)
Take for example https://www.easycron.com/ (no affiliation but the first Google result). You can setup a CRON job there to call a URL to trigger those tasks on a CRON interval. Internally at my company called the "poor mans CRON" :)
I would agree that running a "screen" session is the most unreliable since on a server reboot those are not started again and if you "infinite loop" crashes it will not automatically restart.
If you go that route (or any CRON route) you can add some monitoring using for example https://healthchecks.io/ (again no affiliation, Google). This allows you to define a CRON schedule and gives you a URL to call after the CRON finishes, if your CRON does not call that URL according to the CRON schedule you will be notified. Good to have as insurance.

How to track back where a script is scheduled in crontab

I have scheduled one shell script in cron that fires emails depending on the condition.
I have modified the sender email address.
Now , the issue is while i tested the script in different test environments, somewhere probably it is still active and firing emails from test environment. I have checked crontabs in test environments but nowhere i found out it is scheduled.
Can you guys help me on how to track back where from those emails getting triggered? which instance? which cron etc.
Thanks in advance.
I suggest consulting the cron log file. This file records when and what programs cron starts on behalf of which user.
For FreeBSD this is /var/log/cron. On Linux, as always, it may depend on your distro, the cron implementation, phase of the moon :-) Running man cron might point you at the right file in the FILES section.

Will cron run for different user or account?

I'm new to cron. Someone just introduced it to me. Will cron run for different users?
I mean, I want to run a certain script (which update the database) every 5 minutes if the user is online. What if I have a lot of users who are online? Can cron handle a task like this?
Use crontab -e to edit the current user's crontab. However the script will have to check if the user is online or not.
Cron won't do that automatically.

Run program in background, constantly, on a web server - preferably in PHP

I want to create a website application, that will allow our members to get text message/email alerts every day 1 hour before their lesson (a reminder).
My server-side language background is strictly in PHP (although I've tampered some c++ back in the day). For this to work, obviously, I'll needs to somehow run a program constantly on my server.
Can this be accomplished in PHP?
If yes, is php efficient at this?
If not, how can I do this?
Or, maybe, this an entirely wrong approach, and there's a better way of creating such a service.
yes, u can consider make PHP as a daemon
or check this out php execute a background process
or simply use cron - http://en.wikipedia.org/wiki/Cron
but you should NOT create a web service/application just to run background PHP processes, it should cater for complex job
Sure, you can use PHP as a scripting language on the server. It's just as capable as any other.
Write a PHP script that checks your database for what members need to be alerted, then sends the message. Add a crontab to run this script every minute/hour/whatever. To run a php script from the command line, you run the php interpreter and give it the script name to run.
$ php /path/to/script.php
You would have to start a service on the server itself or create a CRON job to run at any given interval. If you don't have admin privileges you will have to do a CRON job, which can usually be setup in your host's cpanel.
For instance, you could create a small PHP script that
1) Searched for all lessons that start in the hour proceeding the current hour. So if the script is run at 5pm it would search for lessons that start between 6pm and 6:59.
2) Send an email to those members.
It wouldn't be exactly 1 hour though.

Resources