We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36760
    • 136 Posts
    I put together a plugin that sends an email to users once their account is 30 days old. My original plan was to just have the plugin run on some common System Event (like OnWebPageInit), but I realized I really only need it to run once per day. Is there a way to do that?

    If there isn't a way to do that, will there be any performance issues if I have a plugin that cycles through all users in a specific group running that often? I don't know how many users there will be yet, I assume that's the biggest factor.

    Thank you for the help and insight.

    This question has been answered by chris.dempsey78. See the first response.

    • discuss.answer
      • 5160
      • 118 Posts
      chris.dempsey78 Reply #2, 9 years ago
      • You could use a plugin that checks the time at a given event, and only checks the user accounts when the time is right. You can use the PHP date("G") function to determine the hour. This would not be exact, and would depend on your site getting at least one hit during that hour.
        if(date("G") == 23) { ...

        If you need a more exact and dependable action, you need to look into cron. Scheduler is a cron manager; there is at least one other, CronManager comes to mind. Some of the newsletter add-ons have cron managers built-in.
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 36760
          • 136 Posts
          Thank you both. I was so set on finding some code to add to my own plugin that I didn't even think of looking for other plugins.

          I ended up going with the Scheduler plugin so it runs consistently. We'll find out tomorrow if it works! **EDIT: It worked as expected, my code was triggered by Scheduler and sent the email. [ed. note: firebot6 last edited this post 8 years, 11 months ago.]
            • 3749
            • 24,544 Posts
            It's usually pretty easy to set up a cron job using cPanel now. Later versions of cPanel have a pretty friendly interface for it. You just set when it should run and type in the path to the executable PHP file. In your case, you'd probably have to instantiate MODX in the code, but that's also relatively simple. See this.

            Doing it with a cron job has the added advantage of not slowing down any page loads on the site. You can also put the PHP file above the MODX web root for greater security, and the process doesn't depend on anyone visiting the site so you can run it in the middle of the night.
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 5160
              • 118 Posts
              Quote from: BobRay at May 10, 2015, 09:17 PM
              It's usually pretty easy to set up a cron job using cPanel now. <snip>
              Same applies to Plesk control panel running on Windows, hooks up to Windows Task Scheduler. Useful if you don't have root access to work directly with WTS.
              • Glad to see you already found Scheduler smiley

                Did you add a task that just repeats itself every night that goes through all users to check their signup date? Because if you did, I do have another idea for you that might be interesting based on what we have set up on modmore.com..

                Rather than looping over all users, we have a task signup_reminder_14d that we schedule in a hook during the signup. When calling $task->schedule() we pass it the second param with the user ID, like so:

                $task->schedule('+14 days', array('user' => $userId));


                In the signup_reminder_14d task we then grab that users' email and send him a reminder if necessary.

                This way we can see via the scheduler component what exactly happened or is going to happen, and users also get the notification at roughly the same time they signed up, rather than in the middle of the night. Approaching it like that makes it more like a message queue rather than just an interface for cron jobs, so I just wanted to give that perspective wink
                  Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                  Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
                  • 36760
                  • 136 Posts
                  Thank you for the insight. I'll look a little more into it once this project settles down more.

                  I thought I had something setup that was running daily, but I actually had it setup to run once in about a day. What's the correct method to get it to run daily? I might be missing something obvious, but I looked all over.
                  • Indeed a run is not automatically repeated. To do that, call $task->schedule near the top of your task to repeat it, passing an optional data array as second argument:

                    $task->schedule('+1 day', $data);
                      Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                      Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.