Crontabbed shell script not able to create/write to a file

Nikster2014

I have written a simple shell script to backup the tables in my DB using the mysqldump command. The command simply dumps the DB into a dbName.sql file. However, if the dbName.sql file doesn't already exists and I crontab this shell script, it doesn't work. What could be the reason? Are crontab scripts not permitted to create and write to files?

Runium

Cron jobs can be run as root or another specified user. If that user does not have the permission to write to the .sql file the job will fail.

If the file exist and it has write permission for the user running the job the file will be written.

if the file does not exist and the user does not have permission to create files in target directory, the job will fail.

I can typically say

sudo touch /var/log/foo.log
sudo chown my_username /var/log/foo.log

now I can say:

echo bar > /var/log/foo.log

but if the file does not exist it will fail, as I have no permission to create files in /var/log/.

So question becomes if the user running the job have the right to create files where ever the output from mysqldump is written.


Now if I set up a cron job to run a script with this:

date > /var/log/foo.log

every minute, I can add:

*  *    * * *   my-user-name  /path/to/script

to /etc/crontab. As I have no permission to create files in /var/log/ the cron daemon will send me an email when the job fails.

After 1 minute there are no file. I go to a terminal and type mail:

$ mail
>N  1 Cron Daemon        Sat Jan 31 05:15   19/731   Cron <user@host>    /path/to/script
? type
...

/path/to/script: line 3: /var/log/foo.log: Permission denied

OK. My bad. I create the file and chown it to me. Now all is OK.


Have a look at these as well:

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Bash : Taking a wildcard as argument of a shell script and being able to expand it

分類Dev

Expect script not able to read mkdir from file and execute

分類Dev

How to run a shell script from another shell script reading first shell script name from a CSV file

分類Dev

Parse hocon file using shell script

分類Dev

Store in a variable a path read in a file (script shell)

分類Dev

Reading a file in a shell script and selecting a section of the line

分類Dev

Shell script to validate a csv file column by column

分類Dev

Shell script to validate a csv file column by column

分類Dev

Extract line from a file in shell script

分類Dev

Convert batch file for loop to shell script for loop

分類Dev

Open a file type with shell script in OS X

分類Dev

Run custom shell script '[cp] embed pods frameworks' no such file or directory

分類Dev

Create a Linux shell script program that sorts the numbers and words in a file?

分類Dev

shell script to match column and add new columns into table from file

分類Dev

generating a output file by comparing the 2 files in shell script

分類Dev

Inserting text at begining of each line of text file using shell script

分類Dev

Search specific text from file and output the line (Shell script)

分類Dev

How to read and split comma separated file in a bash shell script?

分類Dev

Shell Script: Using files based on part of file name

分類Dev

awk shell script to process a csv file with double quotes

分類Dev

Shell script to compare two specific rows in a single CSV file

分類Dev

Replace a string in a text file using a shell script not working properly

分類Dev

How to get directory and file names while looping in shell script

分類Dev

editing file through vi editor in shell script show error

分類Dev

Using a shell script to add a ‘</pre>’ after the last line in a large file

分類Dev

Why is my shell script putting arbitrary numbers in this file?

分類Dev

How to change file text content only with Shell Script commands?

分類Dev

read multiple values from a property file using bash shell script

分類Dev

Shell script to update the date (T ) with T+2 in a text file

Related 関連記事

  1. 1

    Bash : Taking a wildcard as argument of a shell script and being able to expand it

  2. 2

    Expect script not able to read mkdir from file and execute

  3. 3

    How to run a shell script from another shell script reading first shell script name from a CSV file

  4. 4

    Parse hocon file using shell script

  5. 5

    Store in a variable a path read in a file (script shell)

  6. 6

    Reading a file in a shell script and selecting a section of the line

  7. 7

    Shell script to validate a csv file column by column

  8. 8

    Shell script to validate a csv file column by column

  9. 9

    Extract line from a file in shell script

  10. 10

    Convert batch file for loop to shell script for loop

  11. 11

    Open a file type with shell script in OS X

  12. 12

    Run custom shell script '[cp] embed pods frameworks' no such file or directory

  13. 13

    Create a Linux shell script program that sorts the numbers and words in a file?

  14. 14

    shell script to match column and add new columns into table from file

  15. 15

    generating a output file by comparing the 2 files in shell script

  16. 16

    Inserting text at begining of each line of text file using shell script

  17. 17

    Search specific text from file and output the line (Shell script)

  18. 18

    How to read and split comma separated file in a bash shell script?

  19. 19

    Shell Script: Using files based on part of file name

  20. 20

    awk shell script to process a csv file with double quotes

  21. 21

    Shell script to compare two specific rows in a single CSV file

  22. 22

    Replace a string in a text file using a shell script not working properly

  23. 23

    How to get directory and file names while looping in shell script

  24. 24

    editing file through vi editor in shell script show error

  25. 25

    Using a shell script to add a ‘</pre>’ after the last line in a large file

  26. 26

    Why is my shell script putting arbitrary numbers in this file?

  27. 27

    How to change file text content only with Shell Script commands?

  28. 28

    read multiple values from a property file using bash shell script

  29. 29

    Shell script to update the date (T ) with T+2 in a text file

ホットタグ

アーカイブ