How to track back where a script is scheduled in crontab - linux

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.

Related

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.

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.

Jenkins to monitor external cron jobs

We are using a dedicated Amazon Ubuntu ec2 instance as Cron server, which executed 16 cron jobs at different time intervals i.e, 10 cron jobs in morning 4:15 - 7:15 and the rest # 23:00 - 23:50. I get the results via email. I want to configure something, which shoots email message at the end of they day listing the cron jobs that are executed successfully and the one that failed.
I have a jenkins configured ubuntu instance for auto-building Dev, Beta, Staging & Live environments. Can i add these cron jobs(shell scripts) as external jobs in the jenkins and monitor them. Is it possible?
Definitely possible! You can monitor external cron jobs as described here:
https://wiki.jenkins-ci.org/display/JENKINS/Monitoring+external+jobs
You can also add cron job (-like behavior) to Jenkins by creating a freestyle software project and add "Execute shell" as build process.
It's a bit more convenient since you can also trigger the execution via Jenkins ("Build now").
You might be able to combine the Jenkins monitor external job project type with a matrix project. At the very least the former will enable you to monitor the cron jobs individually.
Alternatively you could have the last monitored cron job of the day trigger building a project that checks the status of all the cron jobs (for example by retrieving and comparing the build numbers of the last and the last successful builds) and sends an email accordingly. The email plugin might be useful for the latter.
Check the CPAN or do some web digging for shell or perl script for managing cron jobs and extend its behaviour to do some reporting which you can render using HTML. Alternatively write a servlet and a some function calls to do just that.
This becomes your own standalone monitor application, which can sit in jenkins or deployed independently. If you choose to add it to jenkins, then add the reporting HTML file and its scripts to the container holding deployed web files for jenkins, word of advice place your files and script in a separate container.
Add a hyperlink to jenkins index html which will load your reporter. Now reboot tomcat and go from there.
Another option could be to take a look at Cronitor (https://cronitor.io). It basically boils down to being a tracking beacon that uses http requests to ping when a cron job/scheduled task starts and ends.
You'll be notified if your job doesn't run on schedule, or if it runs for too long/too short, etc. You can also configure it to send alerts to you via email, sms, but also Slack, Hipchat, Pagerduty and others.

Is there a limit excution time when I run a PHP Script by Cron Jobs?

Is there a limit excution time when I run a PHP Script by Cron Jobs?
For example, I need to backup my big database file through a PHP Script. I set a cronjobs which automatically run everyday.
My question, will the php script run until everything is ok?
ini_set("max_execution_time", "0");
ini_set("max_input_time", "0");
set_time_limit(0);
How I usually set up my cron job scripts is to simply printing everything (especially errors or exceptions) to standard out. To my experience most cron systems will then email the output of the script to whatever local user is running the script. I then have that mail forwarded to my work account to notify me that the script ran and if there were any errors. For basic maintenance scripts that run daily or weekly I've found this to be a quick, effective way to keep me updated on their status.

Wordpress Cron: Running as which user?

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.

Resources