There are two files. The first is the python code (say_hello.py):

def write_text(text): with open('text.txt', 'a') as the_file: the_file.write(text + '\n') if __name__ == '__main__': write_text('bla-bla') 

The second is the shell script that runs this file (hello.sh):

 #!/bin/bash python3 /home/ubuntu/say_hello.py 

And there is a cron that runs the sh file every minute ($ sudo crontab -e):

 * * * * * /home/ubuntu/hello.sh 

Why does nothing work?)

  • Now, if you had a mail server configured in your system, for example, Exim or postfix, then cron would send a letter to the user (on whose behalf the script runs) an email in which it would be possible to get the details. And so vangyu that there is no right to create a file or write to it. - de_frag
  • It's not about rights. he doesn’t even want to do the usual print ("Hello"). - Maxon
  • one
    And where do you expect to see the output of print (“hello”)? - de_frag
  • Well, only in the terminal, there will be no place for him anymore. - Maxon
  • one
    And there will be nothing. You can try a simpler version in crontab: * * * * * echo “hello”. If there is an MTA in the system, a letter will come with the word hello. Or in your case - silence. - de_frag 4:03 pm

1 answer 1

diagnostic steps

  1. Determine if the cron program is running at all (an example of the output is shown):

     $ pgrep -ax cron 379 /usr/sbin/cron -f 
  2. Determine whether the user on whose behalf you are going to perform tasks is allowed to do this (the root usually has no restrictions).

    if there is a file /etc/cron.allow , then the user should be mentioned in it. if this file does not exist, but there is /etc/cron.deny , then the user should not be mentioned in it.

    if both files are not present, then usually all users are allowed to create tasks (using the crontab program).

  3. by running from the right user

     $ crontab -e 

    create a minimum proofing task. eg:

     date >> /tmp/какой-нибудь-файл 

    specify the name (for now) of a non-existent file. specify the nearest minute as the time. or, in order not to philosophize slyly, specify asterisks, but do not forget to delete this task later:

     * * * * * date >> /tmp/какой-нибудь-файл 

    if a few seconds after the next minute, the file you specified was not created, it is likely that your system still in some way prohibited the use of cron (see the last sentence of the previous paragraph) and you should contact the system administrator.

    if the file was created but empty, it makes sense to specify the full path to the date program (and further indicate the full paths to the programs, including the scripts that you will run using the cron program).

  4. in more complex tasks, it makes sense to redirect not only stdout , but also stderr to the file:

     какая-нибудь команда >> /tmp/файл 2>&1 

    so that the error messages sent by many programs to stderr were also written to it.


additional reading:

  • $ man cron
  • $ man crontab
  • $ man 5 crontab