Q:

Command fails in crontab but works in terminal

I have checked this question regarding crontab failures. 
I am running this command to capture the htop command and create a snapshot of it in html format (htop output to human readable file):
/bin/echo q | /usr/bin/htop | /usr/bin/aha --black --line-fix > /tmp/htop.html

This command works correctly on terminal and it doesn't depend on any environment variables.  When I add this command as a cronjob, it just generates an empty html file:
<? xml version="1.0" encoding="UTF-8" ? >
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--  This file was created with the aha Ansi HTML Adapter.  http://ziz.delphigl.com/tool_aha.php -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="application/xml+xhtml; charset=UTF-8" />
<title>stdin</title>
</head>
<body style="color:white; background-color:black">
<pre>
</pre>
</body>
</html>

Any reason why it is not able to capture the output via cron? 

A:

htop needs the TERM variable set to e.g. xterm to output any data, so just set this variable for xterm in your cron line, e.g.:
@hourly echo q|TERM=xterm htop|aha --black --line-fix >/tmp/htop.html

If you want to test whether a command really doesn’t depend on any environment variables – which often lead to problems with cron, see the question you linked –, use env -i to “start with an empty environment”:
$ env -i htop
Error opening terminal: unknown. 

Combined with the diff of cron’s and your terminal’s environment that’s a nice way to test which variables a command needs.  htop in fact only needs TERM:
env -i TERM=xterm htop

