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;
}