2016-06-24 9 views
0

Bash-Skript,Linux cron-Job zu erstellen Datei mit Python

#! /bin/bash 

echo 'hi bash' 

aa 
python ~/fledgling/cron/test.py 

echo 'after python script' 

hier aa alias ist, die

alias aa='cd /home/fledgling/projects/myblog/base' 

Python-Skript,

import time 

with open('~/fledgling/cron/%s.txt' % time.strftime("%Y_%m_%d__%H:%M:%S"), 'w') as f: 
    f.write('%s' % time.strftime("%Y_%m_%d__%H:%M:%S")) 

Ich möchte virtualenv aktiviert, um zu Führen Sie dieses Python-Skript für jede Minute aus.

crontab,

* * * * * ~/fledgling/cron/test.sh >> ~/fledgling/cron/log.txt 

Inhalt von ~/junge/cron/log.txt,

hi bash 
after python script 

Ausgabe von/var/log/syslog

✘ ✝ ~/fledgling/cron tail -f /var/log/syslog 
Jun 24 13:46:01 mysys CRON[14697]: (mysys) CMD (~/fledgling/cron/test.sh >> ~/fledgling/cron/log.txt) 
Jun 24 13:46:01 mysys CRON[14696]: (CRON) info (No MTA installed, discarding output) 
Jun 24 13:47:01 mysys CRON[14736]: (mysys) CMD (~/fledgling/cron/test.sh >> ~/fledgling/cron/log.txt) 
Jun 24 13:47:01 mysys CRON[14735]: (CRON) info (No MTA installed, discarding output) 
Jun 24 13:48:01 mysys CRON[14747]: (mysys) CMD (~/fledgling/cron/test.sh >> ~/fledgling/cron/log.txt) 
Jun 24 13:48:01 mysys CRON[14746]: (CRON) info (No MTA installed, discarding output) 
Jun 24 13:48:11 mysys crontab[14769]: (mysys) BEGIN EDIT (mysys) 
Jun 24 13:48:59 mysys crontab[14769]: (mysys) END EDIT (mysys) 
Jun 24 13:49:01 mysys CRON[14794]: (mysys) CMD (~/fledgling/cron/test.sh >> ~/fledgling/cron/log.txt) 
Jun 24 13:49:01 mysys CRON[14793]: (CRON) info (No MTA installed, discarding output) 

Berechtigungen,

-rw-rw-r-- 1 mysys mysys 476 Jun 24 13:57 log.txt 
-rwxrw-r-- 1 mysys mysys 153 Jun 24 13:37 test.py 
-rwxrwxr-x 1 mysys mysys 91 Jun 24 13:32 test.sh 

Was ich will? Ich möchte jede Minute eine Textdatei erstellen.

Was ist mein Problem? Ich kann im Protokoll sehen, dass nach jeder Minute läuft Bash-Skript ohne Fehler, warum Python-Skript nicht ausgeführt wird oder keine Textdatei erstellen?

+0

'#!/bin/bash' sollte kein Leerzeichen sein. '~' Wo glaubst du das? – 123

Antwort

1

Das Problem ist, dass Sie nicht ~ in Python, which will be expanded in Shell nicht Python verwenden können.

Da von einigen Antworten erwähnt, eine Sache zu erklären ist

Ein Raum nach #! es ist optional.

Ich sah dies von wikipedia, und ich habe überprüft, dass auf meinem Mac, es stimmt.

0
  • Nein ~ in Python
  • nach Keinem Platz! in bash-Header
  • Nicht sicher von cron gestartet bash Ihre alias
  • Nicht sicher crontab akzeptieren ~
  • Blick auf Ihre Log erraten (/ var/log/syslog vielleicht)
  • Versuchen umleiten 2>/tmp/debug.log in crontab
  • überprüfen, ob test.sh ausführbar ist
0

wenn Sie das Skript auf dem Server läuft, dann können Sie nicht verwenden werden:

os.path.expanduser('~') 
0

Verwenden Sie in Ihrem Shell-Skript den vollständigen Pfad und nicht einen Alias.

aa =/home/junge/projects/myblog/base

cd $ aa