1

bash script,

#! /bin/bash

echo 'hi bash'

aa
python ~/fledgling/cron/test.py

echo 'after python script'

here aa is alias, which activates virtualenv

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

python script,

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"))

I want to to run this python script for every one minute.

crontab,

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

contents of ~/fledgling/cron/log.txt ,

hi bash
after python script

Output of /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)

Permissions,

-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

What I want ? I want to create text file every minute.

What is my problem ? I can see in log that after every minute bash script is running with no error, then why python script is not getting executed or not creating any text file ?

1
  • #! /bin/bash should be no space. ~ Where do you think that is going? Commented Jun 24, 2016 at 8:41

4 Answers 4

1

The problem is that you can not use ~ in python, which will be expanded in shell not python.

Since mentioned by some answers, one thing to declared is

A space after #! is optional.

I saw this from wikipedia, and I have checked that on my Mac, it's true.

Sign up to request clarification or add additional context in comments.

Comments

0
  • No ~ in Python
  • No space after ! in bash header
  • Not sure bash launched by cron will guess your alias
  • Not sure crontab accept ~
  • Look at your log (/var/log/syslog maybe)
  • Try redirect 2>/tmp/debug.log in crontab
  • check if test.sh is executable

Comments

0

If You are running that script on server then You may not be using:

os.path.expanduser('~')

Comments

0

In your shell script use the full path rather then an alias.

aa=/home/fledgling/projects/myblog/base

cd $aa

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.