2

I have this question 'is it possible to run a curl script that fetches data from a server, in the background, without loading the webpage containing the curl script ?'

I have a curl script that retrieves json from a server. But for it to retrieve data, I need to keep the webpage open all the time that contains the curl code. This doesn't seems a practical solution.

XCeptable
  • 1,167
  • 5
  • 23
  • 43
  • 2
    Cron job might be what you are looking for? – Andreas Oct 23 '18 at 11:11
  • Maybe AJAX Call? – donald123 Oct 23 '18 at 11:12
  • To run the script without loading webpage, we have the option of the cronjob. In that, we need to provide time interval (how many time in that time interval script should run i.e. every 5 min, every 1 hour, every week etc.) You can find how to set time intervals – Praveen Kumar Oct 23 '18 at 11:50
  • @PraveenKumar: Can you please guide little more. Lets say I put cronjob to run the webpage each 2 minutes like 02:30 i.e. each 30 second of 2nd minute so it fetches and run query to insert data into database. Then I think the web page needs to be closed too. How does this setup will work. – XCeptable Oct 24 '18 at 07:48
  • @XCeptable :- Cronjob is work in the background. There is no any relation in the webpage in a cronjob. There no any dependency in the web browser. Can you please let me know where are you set up cron and how? by CPanel, VPN or CMD? – Praveen Kumar Oct 24 '18 at 08:04
  • I am thinking to set it using crontab command on cmd – XCeptable Oct 24 '18 at 12:25
  • I want it to execute the script each five minute, 30 seconds pats each 5 minute starting from 3rd minute of the hour. Like 15:03:30 -> 15:08:30->15:13:30->15:18:30 ..... and continue indefinite – XCeptable Oct 24 '18 at 12:28

1 Answers1

1

To run the script without loading webpage, we have the option of the cronjob. In that, we need to provide time interval (how many time in that time interval script should run i.e. every 5 min, every 1 hour, every week etc.) You can find how to set time intervals

https://crontab.guru/

Cront file setup code (Linux Crontab Syntax):-

0 1 * * 0 wget https://example.com/test.php >> /dev/null #Run time At 01:00 on Sunday

For crontime check here :- How to set a cron job to run at a exact time?

Setup Cron in Linux(CMD):- crontab -e Link :- https://tecadmin.net/crontab-in-linux-with-20-examples-of-cron-schedule/

Praveen Kumar
  • 779
  • 1
  • 8
  • 29
  • */04 * * * * /bin/sh /var/www/html/myscript.php as IO don't find any seconds options, I can use minutes only. Do you think this command will run myscript.php file in the root directory every 4th minute (4,8,12,16,...) indefinitely ? – XCeptable Oct 24 '18 at 13:01
  • @XCeptable You can **sleep** Check here https://stackoverflow.com/questions/9619362/running-a-cron-every-30-seconds – Praveen Kumar Oct 25 '18 at 04:08