2

I am using a headless raspbian OS on Raspverry Pi 3B+.

I have placed the following code at the home directory of the user pi

#!/usr/bin/env python
import socket
import httplib

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
#print(s.getsockname()[0])    

c = httplib.HTTPSConnection("mywebpageurl.com")
c.request("GET", "/?IP" + s.getsockname()[0] )
response = c.getresponse()
data = response.read()
s.close()

After typing crotab -e in the terminal, I have placed the following code for the cronjob

@reboot sh /usr/bin/python /home/pi/test.py

My problem is when I run the python file seperately, my server receives the GET parameters. However when the same happens over a reboot using cronjob, the GET parameter is missing.

My question is specific. Why do the GET parameters go missing?

I have faced the same issue with wget and curl as well. When I call them over my terminal, the script works. When the script is called by crontab, it doenst work.

P.s. I wrote a c program as well. It works perfecly when run from the terminal. But when I run it through crontab, it again doesn't work. THis is really confusing.

#include <stdio.h>
#include <unistd.h>
#include <string.h> /* for strncpy */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <mysql/mysql.h>


int main() {
 int fd;
 struct ifreq ifr;

 fd = socket(AF_INET, SOCK_DGRAM, 0);

 /* I want to get an IPv4 IP address */
 ifr.ifr_addr.sa_family = AF_INET;

 /* I want IP address attached to "eth0" */
 strncpy(ifr.ifr_name, "wlan0", IFNAMSIZ-1);

 ioctl(fd, SIOCGIFADDR, &ifr);

 close(fd);

 /* display result */
// printf("%s\n", inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));


//printf("MySQL client version: %s\n", mysql_get_client_info());

  char query[2000000];

  MYSQL mysql;

  mysql_init(&mysql);


 if (!mysql_real_connect(&mysql,"13.ip.ip.ip", "name", "password", "ipAddresses", 0, NULL, 0)) { 
                fprintf(stderr, "Failed to connect to database: Error: %s\n",
                mysql_error(&mysql));
}

 // printf ("%s \n\n\n",inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
  snprintf (query,200,"INSERT INTO `IP` (`ip`) VALUES ('%s');", inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
//  printf ("The Query is : %s",query);  
  mysql_query(&mysql, query);
  mysql_close(&mysql);

  return 0;



}
1
  • 1
    For the sake of learning as well as finding out why it's not working from Cron _ I'd suggest putting some debug output lines into your python script. Run from command line and capture the output to a file. Then make your crontab entry pipe the results to a file and run it from cron. You'll be able to compare the results. don't forget to capture the error stream when running from Cron. Another suggestion is to make your .py script executable: so you won't need the /usr/bin/python part of the command in your crontab.
    – Charemer
    Commented Oct 30, 2018 at 15:45

1 Answer 1

4

The problem is that cron doesn't wait for your network to come up before it runs @reboot. The explanation as to why cron operates this way, and alternatives to cron are verbose, but have all been documented in this forum.

Fortunately, the simple solution is not complicated! You must simply tell cron to wait a few seconds before it executes your script. This should resolve your issue:

@reboot /bin/sleep 30; /usr/bin/python /home/pi/test.py  

30 seconds of sleep should be plenty, and you may be able to do with less. Experiment with different values to find what works best for you.

2
  • Hi, this answer is golden. But it seems even 30 seconds are not enough and 100 seconds are required. I feel this is crazy! But thanks for your answer. I would have never figured this out myself.
    – Denis
    Commented Oct 31, 2018 at 4:15
  • 1
    That 100 seconds are required is most likely a reflection on the router/server from which your RPi gets its DHCP information. A 3B+ will "normally" boot up very quickly.
    – Seamus
    Commented Oct 31, 2018 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.