Need linux/python help with writing to csv file in cron

In summary: I agree, Python is a great language for developers. It's very readable and has a huge variety of libraries available.
  • #1
devadmin
Admin
140
1
I'm writing a simple script that writes to a csv file. When ran from console it works fine. When ran with cron, it doesn't write. No errors in the log. All files and folders are 777. The cron is active and running. What am I missing?
 
Technology news on Phys.org
  • #2
What do:
Code:
$ dmesg
$ cat /var/log/syslog
say?
If there was an error, it should show up in one of those.

How do you know that the script is actually running?

I recommend to start the script with something like
Code:
#!/bin/bash
logger "Running simple script that writes to csv file"
so that there will always be an entry in the syslog. Note that standard output does not go anywhere.
If after that, there is still no entry in the log (after waiting for the script to trigger), that implies that the script did not actually run.
 
  • #3
oh derp, I had a relative path to the file :D
 
  • #4
I have found cron to be extremely picky about security settings, and not very helpful at steering you to set them correctly.
 
  • #5
Seems like a good place to ask this, how many of you use Python at work or for personal projects? 3 years ago I had never once touched it now it’s my go to language. Still not my best but the dominance it has in data science plus the huge variety of tasks it can do are very appealing.
 
  • #6
Jameson said:
Seems like a good place to ask this, how many of you use Python at work or for personal projects? 3 years ago I had never once touched it now it’s my go to language. Still not my best but the dominance it has in data science plus the huge variety of tasks it can do are very appealing.
Agreed, I'm just a few months in and I love it. It's so clean. Looking at nested indents can take getting used to. There are also so many libraries, great community.
 
  • #6
The nested part was weird for me at first but with a good editor like Sublime the coloring and formatting help make it very obvious what code goes to what chunk.
 
  • #7
I'm still slightly better at LabVIEW (and was therefore extremely excited when NI put out the Community Edition), but I like many things about Python. As text-based languages go, it's my favorite, because it appears optimized for developer time.

I do all my projects using various databases (either relational with SQL, or now I'm starting to use Neo4J with Cypher) and Python. That's it.
 

1. How do I write to a CSV file in a cron job?

To write to a CSV file in a cron job, you can use the Python csv module. First, import the module and open the CSV file using the csv.writer function. Then, use the writerow() method to add data to the CSV file. Finally, close the file when finished.

2. How do I format the data in the CSV file?

The csv.writer function has several optional parameters that allow you to specify the delimiter, quote character, and other formatting options for the CSV file. You can refer to the Python documentation for more details on how to use these parameters.

3. Can I schedule the cron job to run at a specific time?

Yes, you can use the crontab command to schedule the job to run at a specific time. The syntax for scheduling a cron job is minute hour day_of_month month day_of_week command. You can refer to the cron documentation for more information on how to set up a cron job.

4. How can I troubleshoot if my cron job is not working?

If your cron job is not working, there are a few steps you can take to troubleshoot the issue. First, check the job's output for any errors. You can also check the cron log file for any error messages. Additionally, make sure the correct path to the Python interpreter is specified in the cron job command.

5. Is it possible to write to multiple CSV files in a single cron job?

Yes, you can write to multiple CSV files in a single cron job by repeating the steps for each file. Alternatively, you can use a loop to iterate through a list of CSV files and write data to each one. Just make sure to close each file after writing to it to avoid any data loss.

Similar threads

  • Programming and Computer Science
Replies
9
Views
862
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
2
Views
381
Replies
16
Views
2K
  • Programming and Computer Science
Replies
2
Views
21K
  • Programming and Computer Science
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top