baffling cron thing on Raspberry Pi - cron

I am running a cron on a Pi. It basically works ... except .. that every now and again the system misbehaves and stops doing the scheduled things. When I log in to it I discover that the crontab has picked up a new line .....
The crontab looks like .....
#reboot .... dum .. dee .. dum .. all works
10 4-22 * * * do something else that works
but an extra line appears at the end
/root/udevd > /dev/null 2>&1 &
and most of the rest stops running .... if I log in and comment this line out, things revert to normal operation.
any ideas what could make this happen?

I had a Pi with a cell connection and a weak password. The cell connection permitted inwards calls. I got owned ..... just like everyone says.
The use of an open link was not as bad as it sounds - the default mode of operation was to only connect for about two minutes in 20 and each time the IP address changed. Makes it a bit hard for the brute force probes to get far.
To fix it (using a new SD card). I used an APN that does not permit inwards connections. I communicate with the Pi using dataplicity or openvpn.

Related

How to run a python script between a particular times every single day (on Linux)?

I am looking for a way to be able to run a python script at a particular times of day and then have it auto terminated at another time of day. Ideally, I would want this to not be done within the script itself.
For example: I would want the script to start at 08:00 and end at 10:00 then start again at 11:30 and then terminate at 15:00 and I would need this to happen every day automatically.
I have browsed through many suggestions online, and many of them suggested to use cron, however, as far as I can see, cron does not natively offer the functionality of automatically terminating an application.
Others have suggested using cron to start the application at a particular time and then use another cron instance to create a "terminate" file that the program will search for at every loop iteration and if the file is present then the python script will terminate via a sys.exit() function or something, however, this seems quite janky and more of a workaround than a real solution.
You may use Jobber. You will be able to start scripts whenever you want and for the time you want.
Warning : Jobber is not free. You can try it for free though.
Here is the link to Jobber's website.
You could write a script that creates a lockfile with cron (https://unix.stackexchange.com/questions/12815/what-are-pid-and-lock-files-for), and use the lockfile to know what the process ID is, then terminate the process with that id using cron as well
After you have determined that the process name is uniquely identifiable you could do something like this (that's indeed also using cron).
0 8 * * * /path/to/unique_name.py& ( echo "pkill unique_name.py" | at 10:00 )
30 11 * * * /path/to/unique_name.py& ( echo "pkill unique_name.py" | at 15:30 )
Edit 1:
And "name safe" versions (using kill).
0 8 * * * /path/to/unique_name.py& echo kill $! | at 10:00
30 11 * * * /path/to/unique_name.py& echo kill $! | at 15:30

Using nohup to help run a loop of python code while disconnecting from ssh

I'm looking for help running a python script that takes some time to run.
It is a long running process that takes about 2hours per test observation. For example, these observations could be the 50 states of the usa.
I dont want to baby sit this process all day - I'd like to kick it off then drive home from work - or have it run while I'm sleeping.
Since this a loop - I would need to call one python script that loops through my code going over each of the 50 states - and a 2nd that runs my actual code that does things.
I've heard of NOHUP, but I have very limited knowledge. I saw nohup ipython mypython.py but then when I google I get alot of other people chiming in with other methods and so I don't know what is the ideal approach for someone like me. Additionally, I am essentially looking to run this as a loop - so don't know how that complicates things.
Please give me something simple and easier to understand. I don't know linux all that well or I wouldn't be asking as this seems like a common sort of command/activity...
Basic example of my code:
Two files: code_file.py and loop_file.py
Code_file.py does all the work. Loop file just passes in the list of things to run the stuff for.
code_file.py
output = each_state + ' needs some help!'
print output
loop_file.py
states = ['AL','CO','CA','NY','VA','TX']
for each_state in states:
code_file.py
Regarding the loop - I have also heard that I can't pass in parameters or something via nohup? I can fix this part within my python code....for example reading from a CSV in my code and deleting the current record from that CSV file and then re-writing it out...that way I can always select the top record in the CSV file for my loop (the list of states)
May be you could modify your loop_file.py like this:
import os
states = ['AL','CO','CA','NY','VA','TX']
for each_state in states:
os.system("python /dir_of_your_code/code_file.py")
Then in a shell, you could run the loop_file.py with:
nohup python loop_file.py & # & is not necessary, it just redirect all output of the file to a file named nohup.out instead of printing it on screen.

Using flock to lock file for X time

I am a Linux novice and currently developing a security system using Raspberry Pi 3 and MotionEye. To get notifications via e-mail, I am attempting to create a custom shell script that will send an e-mail if there is motion, lock for X minutes, then send another e-mail if there is still motion. However, I am having some difficulties.
I created a simple Python script named "send_email.py" using SMTP that works perfectly fine for sending e-mails when I execute it via command line.
The shell script (named "flock_email.sh") is where I run into troubles in a few regards:
Whenever I run flock_email.sh, it completely overwrites send_email.py. I have tried to change file permission so it is only executable by the user, but it still overwrites.
The flock command/function does not work as I intended or at all. I have looked all over the internet and tried multiple different codes, but none have worked. I have attached my various flock_email.sh scripts I have tried.
Not necessarily a problem, but I am a bit confused on what my "shebang" line should be. For flock_email.sh I have it as "!#/bin/bash", which I believe makes the script it executable, at least according to this. Do I still need to change the permissions via the command "chmod +x flock_email.sh"? The path is /home/pi, which is essentially the main directory of my Pi.
The different solutions I have tried:
In flock_email.sh, I have tried to directly change the file permissions to read-only instead of using flock, having it sleep, then changing the permissions back to allow execution of the file.
Multiple flock_email.sh implementations, as attached.
To summarize:
I need to execute send_email.py before locking the file flock_email.sh.
Once locked, it needs to stay locked for X time.
Does anyone have any pointers or suggestions? I have spent well over 15 hours tinkering with this and feel like I have gotten nowhere!
send_email.py:
#!/usr/bin/env
import smtplib
def send_email():
content = "Message I want to send to specified e-mail."
sender = "e-mail account that will send message"
pword = "password of sender"
receiver = "e-mail account that will receive message"
mail = smtplib.SMTP("smtp.gmail.com",587)
mail.ehlo
mail.starttls()
mail.login(sender,pword)
mail.sendmail(sender,receiver,content)
mail.close()
send_email()
flock_email.sh (1):
#!/bin/bash
(
python /home/pi/send_email.py
flock -e 200
sleep [time in seconds]
)
flock_email.sh (2):
#!/bin/bash
(
python /home/pi/send_email.py
exec 3>/home/pi/send_email.py
flock -x 3
sleep [time in seconds]
exec 3>&-
)
flock_email.sh (3):
#!/bin/bash
python /home/pi/send_email.py
chmod 444 /home/pi/send_email.py # modify to read only for all
sleep [time in seconds]
chmod 755 /home/pi/send_email.py # modify to rwx for owner, r-x for others
The reason why man flock and all posts say to use > is because you're supposed to use a dedicated lock file, typically in /var/lock:
#!/bin/bash
exec 3> /var/lock/motionmail
flock -ne 3 || exit
python /home/pi/send_email.py
sleep 3600
This additionally fixes you sending your email regardless, before you ever check the lock, and aborts new emails instead of queueing them all up.
You choose the lock file name based on the scope you want your lock to have:
If you only want one email per hour, you can use something like /var/lock/motionmail because there's just one per system.
If you want one email for each user per hour, you can use $HOME/.motionmail.lock because there's just one per user.
You can use /home/pi/send_email.py if you want with <, but this implies that you want one email per hour not only for each user, programming language and script copy, but also every time you hit save and replace the file with your editor*
* Editors differ in whether they replace or overwrite a file

Cron Jobs Linux row deletion

I'm running this script;
$query = "SELECT * FROM XXX WHERE email='$Email'";
if($count==1) // fails
if($count==0) // succeeds
If successful
mysql_query ("INSERT INTO XXX (email) values ('$Email'");
Then proceeds onto the next script.
So, it checks to see if you have already ran this script in the past on that account, if you have your email is stored then you can't run this script ever again on that same email.
However, after this script has been processed I want it to delete the row created for the email after 6 hours.
So that after 6 hours they may run the script again.. I've been enlightened that I need to use Cron jobs for this, But I'm not sure how.. Any help is highly appreciated!
Many regards, and thanks in advance.
0 0,6,12,18 * * * /path/to/mycommand
This means starting from hour 0, 6, 12, and 18 the cron job would run. That would be the cron needed to do what you want.
Depending on which linux version you are running you will need to see how to actually create the cron job.
I would think at now +6 hours is a better choice here.

X3270 Connection and Programming

I'm looking at using a X3270 terminal emulator. I have http://x3270.bgp.nu/ looked over this source material and still don't see how to start using the tool or configure it.
I'm wonder how I can open a terminal and connect. Another question is how could I integrate this into a python program?
edit:
here is a snippet:
em = Emulator()
em.connect(ip)
em.send_string('*user name*')
em.exec_command('Tab')
em.send_string('*user password*')
em.send_enter()
em.send_enter()
em.wait_for_field()
em.save_screen("{0}screenshot".format(*path*))
looking at the save screen i see that the cursor hasn't moved? I can move the cursor using
em.move_to(7,53)
but after that i don't get any text sent through. Any Ideas?
Here's what I do; it works 100% of the time:
from py3270 import *
import sys, os
host = "%s" % sys.argv[1].upper()
try:
e = Emulator()
e.connect(host)
e.wait_for_field()
except WaitError:
print "py3270.connect(%s) failed" % (host)
sys.exit(1)
print "--- connection made to %s ---" % (host)`
If you haven't got a network connection to your host, that wait_for_field() call is going to wait for a full 120 seconds. No matter what I do, I don't seem to be able to affect the length of that timeout.
But your user doesn't have to wait that long, just have him kill your script with a KeyboardInterrupt. Hopefully, your user will grow accustomed to success equaling the display of that "--- connection made ..." message so he'll know he's in trouble when/if the host doesn't respond.
And that's a point I need to make: you don't connect to a terminal (as you described), rather you connect to a host. That host can be either a VTAM connection or some kind of LPAR, usually TSO or z/VM, sometimes CICS or IMS, that VTAM will take you to. Each kind of host has differing prompts & screen content you might need to test for, and sometimes those contents are different depending on whose system you're trying to connect to. Your script becomes the "terminal", depending on what you want to show your user.
What you need to do next depends on what kind of system you're trying to talk to. Through VTAM? (Need to select a VTAM application first?) To z/VM? TSO? Are you logging on or DIALing? What's the next keystroke/field you have to use when you're working with a graphic x3270/c3270 terminal? You need to know that in order to choose your next command.
Good luck!
Please read my comment above first - it would be helpful to have more detail as to what you need to do.
After considering that…have you looked at the py3270 package at https://pypi.python.org/pypi/py3270/0.1.5 ? The summary says it talks to x3270.

Resources