Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have a permission to ask a question, You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

Question Bump

Question Bump Logo Question Bump Logo

Question Bump Navigation

  • Contact Us
  • FAQs
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Communities
  • Questions
    • New Questions
  • Tags
  • Users
  • FAQs
  • Contact Us
  • FAQs
Home/ Questions/Q 421
Next
Jhero23
  • 1
Jhero23Scholar
Asked: August 2, 20202020-08-02T10:26:31+00:00 2020-08-02T10:26:31+00:00In: Programming, Technology, Web Development

How to write cron job to run an sql file in shared hosting

  • 1

I recently wanted to execute a cron job to run an sql file in a shared hosting website. In my case, I wanted to reset by database every hour and everyday for a demo website. There are two methods to achieving this;

  • Using the cron job from the cpanel
  • Using an external service to run the cron job for you

METHOD 1

I learnt from this task that running the sql file directly from the cron job is not so efficient. In fact, in my own project, it didn’t work. Therefore, I changed my method. I wrote the script in a php file calling the sql file, while the cron job calls the php file, because it is more convenient that way. Though, if you just want to run a php script, it can be passed to the cron job in the cpanel and hopefully, it works for you.

In the cron job on the cpanel, there is a guide in order to use it properly. For example

/usr/local/bin/php -f /home/(username)/public_html/nameofthefile.php

<?php

$con = mysqli_connect('localhost', 'username', 'password');
if (!$con) {
    die('Could not connect: ' . mysqli_connect_error());
}

$sql = 'DROP DATABASE db_name';
if (mysqli_query($con, $sql)) {
    //echo "Database db_name was successfully dropped\n";
} else {
    echo 'Error dropping database: ' . mysqli_connect_error() . "\n";
}
$createsql = 'CREATE DATABASE db_name';
if (mysqli_query($con, $createsql)) {
    //echo "Database db_name created successfully\n";
} else {
    echo 'Error creating database: ' . mysqli_connect_error() . "\n";
}

$mysql_host = "localhost";
$mysql_database = "db_name";
$mysql_user = "username";
$mysql_password = "password";
# MySQL with PDO_MYSQL
$db = new PDO("mysql:host=$mysql_host;dbname=$mysql_database", $mysql_user, $mysql_password);

$query = file_get_contents("file.sql"); // calls the sql file here

$stmt = $db->prepare($query);

if ($stmt->execute()) {
    echo "Success";
} else {
    echo "Fail\n";
}

then you select the timing you need for this job to run for.

METHOD 2

In my case, this file above didn’t work on the server, but worked on the localhost which was really surprising. The PDO part of the code, didn’t work to be precise, so initially I thought my shared hosting service didn’t enable PDO for my php version which was 7.3 at that time, after confirmation that it was enabled, I then knew I had to find an alternative to the cron job issue. After much researching, I found google cloud platform (serverless computing — not free) so I couldn’t use it, I then found another online service- webcron that could do this. I registered and configured for what I needed. The only major thing you need to supply is the url.

Therefore, i just supplied https://domain-name.com/nameoffile.php. and BOOM, it worked as I expected.

  • 0 0 Answers
  • 383 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
    Leave an answer

    Leave an answer
    Cancel reply

    Browse
    Browse
    What is the capital of Egypt? ( Cairo )

    Sidebar

    Ask A Question

    Inspirational Podcast

    Stats

    • Questions 284
    • Answers 168
    • Best Answers 109
    • Users 90

    Adv 250x250

    • Popular
    • Answers
    • Anonymous

      Can one still find true ...

      • 6 Answers
    • Jay

      For how long can company ...

      • 4 Answers
    • Bellarmine901

      I need help installing FIFA ...

      • 4 Answers
    • Bellarmine901

      Why spend so much on ...

      • 3 Answers
    • Bellarmine901

      How Do I Keep My ...

      • 3 Answers
    • neha afzal
      neha afzal added an answer Digital Marketing is a tool that has developed the way marketing operates. It has created a new look to customization and personalization in marketing tactics and activities. To get increased… October 25, 2022 at 10:52 am
    • Humayoun Mussawar
      Humayoun Mussawar added an answer Nice article thanks for sharing it to get the audit of your website find out the seo experts in Pakistan contact him for SEO services          … October 14, 2022 at 12:34 pm
    • Realtorspk
      Realtorspk added an answer Nice article thanks for sharing it I just write an article on business ideas in Pakistan October 14, 2022 at 12:23 pm
    • Anchal Verma
      Anchal Verma added an answer New Generation People  Won't Bother much March 24, 2022 at 8:08 am
    • Jhero23
      Jhero23 added an answer Thank you very much April 16, 2021 at 8:40 am

    Users

    Daniel

    Daniel

    • 0 Questions
    • 381 Points
    Scholar
    Dee20

    Dee20

    • 0 Questions
    • 362 Points
    Scholar
    Kenny

    Kenny

    • 0 Questions
    • 256 Points
    Teacher

    Trending Tags

    business (7) coronavirus (11) covid-19 (13) fulfillment (3) hackers (4) health (9) life (9) life hacks (7) love (6) money (6) movies (5) nigeria (5) pc (3) relationship (7) safety (6) social media (5) stay safe (3) success (7) survival (3) virus (5)

    Explore

    • Home
    • Communities
    • Questions
      • New Questions
    • Tags
    • Users
    • FAQs

    Footer

    © 2020 Question Bump. All Rights Reserved
    With Love by PianistProgrammer.

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.