Help with correct shutdown command syntax

This is the place for queries that don't fit in any of the other categories.

Help with correct shutdown command syntax

Postby makem » Fri Dec 06, 2013 2:24 pm

I an using a script which starts automatically at boot via an init script. Everything in the script is working ok except for the shutdown commands.

I cannot find the correct syntax to cause a shut down at a given time and as a bonus, at a given time + some minutes before the kill.

I have a bash script which does the same as this script but I wanted to learn some Python. That script does shut down when necessary.

I am new to scripting and have put this together from various scripts I have found posted (grateful for those)

Code: Select all
#!/usr/bin/env python
import os
import subprocess
import time
import signal
import sys
import commands
import smtplib

def sendemail(from_addr, to_addr_list, cc_addr_list, subject, message, login, password, smtpserver='smtp.gmail.com:587'):
        header  = 'From: %s\n' % from_addr
        header += 'To: %s\n' % ','.join(to_addr_list)
        header += 'Cc: %s\n' % ','.join(cc_addr_list)
        header += 'Subject: %s\n\n' % subject
        message = header + message
        server = smtplib.SMTP(smtpserver)
        server.starttls()
        server.login(login,password)
        problems = server.sendmail(from_addr, to_addr_list, message)
        server.quit()

from datetime import datetime, time
now = datetime.now()
now_time = now.time()

if time(19,00) <= now.time() <= time(21,00):

        os.system("shutdown -h time(21,00)")    # may need to find how to add 15
      
        sendemail(from_addr = 'python@myhome.com', to_addr_list = ['me@gmail.com'], cc_addr_list = ['makem@myhome.co.uk'], subject = 'Message from Pi', message = 'AllwaySync backup now', login = 'xx@gmail.com', password = 'xx')

elif time(03,30) <= now.time() <= time(06,00):

                sendemail(from_addr = 'python@myhome.com', to_addr_list = ['me@gmail.com'], cc_addr_list = ['makem@myhome.co.uk'], subject = 'Message from Pi', message = Backing up now', login = 'xx@gmail.com', password = 'xx')
                # Initialise.
                #clean_command = '/usr/bin/rdiff-backup --force --remove-older-than 2W /media/HDD1/shares/mailwasher /media/HDD2/shares/python_backup/mailwasher'
                backup_command = '/usr/bin/rdiff-backup --exclude /media/HDD1/shares/temp /media/HDD1/shares /media/HDD2/shares/python_backup'

                # Clean up old increments.(Not checked if these work yet)
                #rdiff_backup = subprocess.Popen(clean_command, shell=True)
                #rdiff_backup.wait()

                #### Starting the backup ####
                rdiff_backup = subprocess.Popen(backup_command, shell=True)
            rdiff_backup.wait()     # Wait for the backup to finish.

#               sys.exit(os.EX_OK)
#               os.system("shutdown -h 15")

                os.system("shutdown -h 13,45")
            
else: # print "email playtime"
                sendemail(from_addr = 'python@myhome', to_addr_list = ['me@gmail.com'], cc_addr_list = ['me@myhome.co.uk'], subject = 'Message from Pi', message = Playtime now', login = 'xx@gmail.com', password = 'xx')
#exit 0
makem
 
Posts: 3
Joined: Fri Dec 06, 2013 2:11 pm

Re: Help with correct shutdown command syntax

Postby hrs » Sat Dec 07, 2013 12:04 am

There's a lot of stuff in your code that is unrelated to your problem. Try to make a short as possible program that reproduces your problem and write it according to pep8. If you do these things you are likely to get useful responses.
hrs
 
Posts: 86
Joined: Thu Feb 07, 2013 9:26 pm

Re: Help with correct shutdown command syntax

Postby makem » Sat Dec 07, 2013 8:13 am

Sorry, I used Notepad ++ and thought it was ok. This time I have used Geany and reduced the amount of script to a basic level.

Code: Select all
#!/usr/bin/env python
import os
import subprocess
import time
import signal
import sys
import commands
import smtplib

from datetime import datetime, time
now = datetime.now()
now_time = now.time()

if time(19,00) <= now.time() <= time(21,00):
   
   os.system("shutdown -h time(21,00)")    # may need to find how to add 15
   
elif time(03,30) <= now.time() <= time(06,00):
      
      # Initialise.
      
      backup_command = ( '/usr/bin/rdiff-backup --exclude /media/HDD1/shares/
                       temp /media/HDD1/shares /media/HDD2/shares/python_backup')
      
      # Starting the backup #
      
      rdiff_backup = subprocess.Popen(backup_command, shell=True)
      rdiff_backup.wait()     # Wait for the backup to finish.
      

   os.system("shutdown -h 15")
   
   # os.system("shutdown -h 13,45")
   
else:  print "email playtime"
   
sys.exit(os.EX_OK)
makem
 
Posts: 3
Joined: Fri Dec 06, 2013 2:11 pm

Re: Help with correct shutdown command syntax

Postby stranac » Sat Dec 07, 2013 10:33 am

Well, shutdown can take 3 different time formats, and none of those are what you're trying to use.
The possible formats are:
  • now
  • +m (after m minutes)
  • hh:mm (at this time)
So, if you format your shutdown time as one of the accepted options, it should work.
I would also recommend using subprocess instead of os.system().
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: Help with correct shutdown command syntax

Postby makem » Sat Dec 07, 2013 1:02 pm

stranac wrote:Well, shutdown can take 3 different time formats, and none of those are what you're trying to use.
The possible formats are:
  • now
  • +m (after m minutes)
  • hh:mm (at this time)
So, if you format your shutdown time as one of the accepted options, it should work.
I would also recommend using subprocess instead of os.system().


One of the commands in my script is:

subprocess.Popen(clean_command, shell=True)

I did try making 'command' = a time to shutdown but it returned errors. I can't remember what now but I will tray again when I get home when I can see what the syntax should be.
makem
 
Posts: 3
Joined: Fri Dec 06, 2013 2:11 pm


Return to General Coding Help

Who is online

Users browsing this forum: plas4844 and 3 guests