Viewed   76 times

every Monday I need to empty few fields of a MYSQL database I have

Firstly I thought about using a cron job, but my Web Hosting provider (fatcow.com) doesn't support neither SSH or Command Lines into shared servers.

I've also thought about checking the date() to see if it's monday and perform something, but then I need to run that specific page continuously and I've read that it's a problem.

So now I ask you, how can I do this ?

Thanks for answers

 Answers

4

If your job can reached via a URL, you might try to sign up with a free crontab service like onlinecronjobs.com

Sunday, October 2, 2022
 
lcmylin
 
1

crontab command usage

usage:  crontab [-u user] file
        crontab [-u user] [ -e | -l | -r ]
                (default operation is replace, per 1003.2)
        -e      (edit user's crontab)
        -l      (list user's crontab)
        -r      (delete user's crontab)
        -i      (prompt before deleting user's crontab)

So,

$output = shell_exec('crontab -l');
file_put_contents('/tmp/crontab.txt', $output.'* * * * * NEW_CRON'.PHP_EOL);
echo exec('crontab /tmp/crontab.txt');

The above can be used for both create and edit/append provided the user has the adequate file write permission.

To delete jobs:

echo exec('crontab -r');

Also, take note that apache is running as a particular user and that's usually not root, which means the cron jobs can only be changed for the apache user unless given crontab -u privilege to the apache user.

Tuesday, August 16, 2022
4

Start by typing at a command line:

whereis php

Do this as the user that the cron job will be run under. This will show you the path to your executable. You can then use that path (if it's not already in your PATH variable) in your cron entry:

5 * * * * /your/path/to/php /var/www/some/path/script.php

Edit: you may need to install the php5-cli (Ubuntu package name) package if all you have is the Apache PHP module installed. This will give you the binary executable that you can run from a command line.

Friday, October 7, 2022
5

The following code uses Alarmmanager with BroadcastReceiver which will help you out achieving your need.

In your activity:

Intent intent = new Intent(MainActivity.this, Receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, REQUEST_CODE, intent, 0);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(am.RTC_WAKEUP, System.currentTimeInMillis(), am.INTERVAL_DAY*7, pendingIntent);

System.currentTimeInMillis() - denotes that the alarm will trigger at current time, you can pass a constant time of a day in milliseconds.

Then create a Receiver class something like this,

public class Receiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        showNotification(context);
    }

    public void showNotification(Context context) {
        Intent intent = new Intent(context, AnotherActivity.class);
        PendingIntent pi = PendingIntent.getActivity(context, reqCode, intent, 0);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.android_icon)
            .setContentTitle("Title")
            .setContentText("Some text");
        mBuilder.setContentIntent(pi);
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        mBuilder.setAutoCancel(true);
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(reqCode, mBuilder.build());
    }
}

Also you have to register your BroadcastReceiver class in your manifest file, like the following. In your AndroidManifest.xml file, inside tag,

<receiver android:name="com.example.receivers.Receiver"></receiver>

Here "com.example.receivers.Receiver" is my package and my receiver name.

Monday, October 10, 2022
 
1

You can run PHP code from the command line. e.g., if your PHP folder is in PATH:

php.exe C:mycodemyfile.php

You can then set this up as a scheduled task in windows. Side note: be aware that certain things don't exist (and something exist in their place), e.g. Apache or IIS objects, as well as the full range of HTTP stuff.

Sunday, October 30, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :