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

  • Context: Python 
  • Thread starter Thread starter devadmin
  • Start date Start date
  • Tags Tags
    Csv File Writing
Click For Summary
SUMMARY

The discussion centers on troubleshooting a Python script that writes to a CSV file when executed via cron. The user discovered that the script failed to run due to a relative file path issue, which is a common pitfall with cron jobs. Recommendations included using the logger command to confirm script execution and ensuring absolute paths are used for file operations. The conversation also highlighted the importance of understanding cron's security settings and the need for proper logging to diagnose issues effectively.

PREREQUISITES
  • Basic knowledge of Linux command line operations
  • Understanding of cron job scheduling
  • Familiarity with Python scripting
  • Experience with logging in Linux (e.g., using logger command)
NEXT STEPS
  • Research how to use absolute paths in Python scripts
  • Learn about cron job logging and troubleshooting techniques
  • Explore Python libraries for CSV file manipulation (e.g., pandas)
  • Investigate security settings and permissions for cron jobs in Linux
USEFUL FOR

Python developers, system administrators, and anyone managing cron jobs or working with CSV file operations in Linux environments will benefit from this discussion.

devadmin
Admin
Messages
140
Reaction score
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
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.
 
oh derp, I had a relative path to the file :D
 
I have found cron to be extremely picky about security settings, and not very helpful at steering you to set them correctly.
 
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.
 
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.
 
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.
 
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.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 9 ·
Replies
9
Views
1K
  • · Replies 12 ·
Replies
12
Views
2K
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
22K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
6
Views
3K